From 29013a3f839bf52f378bd50e0e3c3c8829648bf0 Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 1 Jul 2024 22:54:24 +0200 Subject: [PATCH] auto-replace long functions with short names adI = addInfo adD = addDropdown adO = addOption adF = addField before: String buffer usage: 3183 of 4037 bytes after: String buffer usage: 2805 of 4037 bytes --- wled00/data/settings_um.htm | 12 ++++++++++++ wled00/fcn_declare.h | 1 + wled00/util.cpp | 18 +++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/wled00/data/settings_um.htm b/wled00/data/settings_um.htm index 759643e6..19a8590f 100644 --- a/wled00/data/settings_um.htm +++ b/wled00/data/settings_um.htm @@ -146,6 +146,9 @@ urows += `
`; } } + function adF(k,f,o,a=false) { //shortcut for addField(key, field, (sub)object, isArray) + return addField(k,f,o,a); + } // https://stackoverflow.com/questions/39729741/javascript-change-input-text-to-select-option function addDropdown(um,fld) { let sel = d.createElement('select'); @@ -170,6 +173,9 @@ } return null; } + function adD(um,fld) { // shortcut for addDropdown(um,fld) + return addDropdown(um,fld); + } function addOption(sel,txt,val) { if (sel===null) return; // select object missing let opt = d.createElement("option"); @@ -181,6 +187,9 @@ if (c.value == sel.dataset.val) sel.selectedIndex = i; } } + function adO(sel,txt,val) { // shortcut for addOption(sel,txt,val) + return addOption(sel,txt,val); + } //WLEDMM: replace Option to set globals function rOpt(name,el,txt,val) { let obj = d.getElementsByName(name); @@ -285,6 +294,9 @@ if (txt2!="") obj[el].insertAdjacentHTML('beforebegin', txt2 + ' '); //add pre texts } } + function adI(name,el,txt, txt2="") { // shortcut for addInfo(name,el,txt, txt2="") + return addInfo(name,el,txt, txt2); + } // add Help Button function addHB(um) { diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 38a18b09..aa0645d0 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -362,6 +362,7 @@ int getNumVal(const String* req, uint16_t pos); void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255); bool getVal(JsonVariant elem, byte* val, byte minv=0, byte maxv=255); bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255); +void oappendUseDeflate(bool OnOff); // enable / disable string squeezing bool oappend(const char* txt); // append new c string to temp buffer efficiently bool oappendi(int i); // append new number to temp buffer efficiently void sappend(char stype, const char* key, int val); diff --git a/wled00/util.cpp b/wled00/util.cpp index 896b57e3..1bcf2ad0 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -144,22 +144,34 @@ bool oappendi(int i) return oappend(s); } +static bool squeezeStrings = false; +void oappendUseDeflate(bool OnOff) { squeezeStrings = OnOff; } bool oappend(const char* txt) { - uint16_t len = strlen(txt); + String str = squeezeStrings ? String(txt) : String(""); + if (squeezeStrings) { + // simple fixed-dictionary deflate + str.replace(F("addField("), F("adF(")); + str.replace(F("addDropdown("), F("adD(")); + str.replace(F("addOption("), F("adO(")); + str.replace(F("addInfo("), F("adI(")); + } + const char* finalTxt = squeezeStrings ? str.c_str() : txt; + + size_t len = strlen(finalTxt); if ((obuf == nullptr) || (olen + len >= SETTINGS_STACK_BUF_SIZE)) { // sanity checks if (obuf == nullptr) { USER_PRINTLN(F("oappend() error: obuf == nullptr.")); } else { USER_PRINT(F("oappend() error: buffer full. Increase SETTINGS_STACK_BUF_SIZE for ")); USER_PRINTF("%2u bytes \t\"", len /*1 + olen + len - SETTINGS_STACK_BUF_SIZE*/); - USER_PRINT(txt); + USER_PRINT(finalTxt); USER_PRINTLN(F("\"")); errorFlag = ERR_LOW_AJAX_MEM; } return false; // buffer full } - strcpy(obuf + olen, txt); + strcpy(obuf + olen, finalTxt); olen += len; return true; }