Swap jsvGetStringLength()>0 to jsvIsEmptyString(line) - a lot faster

This commit is contained in:
Gordon Williams 2025-05-23 15:25:09 +01:00
parent 1263857ebd
commit e99bdd9541
10 changed files with 19 additions and 19 deletions

View File

@ -4016,7 +4016,7 @@ NO_INLINE void jswrap_banglejs_init() {
bool drawInfo = false;
JsVar *img = jsfReadFile(jsfNameFromString(".splash"),0,0);
int w,h;
if (!jsvIsString(img) || !jsvGetStringLength(img)) {
if (!jsvIsString(img) || jsvIsEmptyString(img)) {
jsvUnLock(img);
drawInfo = true;
img = jswrap_banglejs_getLogo();

View File

@ -2012,7 +2012,7 @@ JsVar *jswrap_graphics_setFont(JsVar *parent, JsVar *fontId, int size) {
unsigned short sz = 0xFFFF; // the actual data mask
if (isVector) {
sz = (unsigned short)size;
} else if (jsvIsUndefined(name) || jsvGetStringLength(name)==0 || jsvIsStringEqual(name, "4x6"))
} else if (jsvIsUndefined(name) || jsvIsEmptyString(name) || jsvIsStringEqual(name, "4x6"))
sz = (unsigned short)(size + JSGRAPHICS_FONTSIZE_4X6);
#ifdef USE_FONT_6X8
if (jsvIsStringEqual(name, "6x8"))
@ -2497,7 +2497,7 @@ JsVar *jswrap_graphics_wrapString(JsVar *parent, JsVar *str, int maxWidth) {
lineWidth += wordWidth;
} else { // doesn't fit on one line - put word on new line
lineWidth = wordWidth;
if (jsvGetStringLength(currentLine) || wasNewLine)
if (!jsvIsEmptyString(currentLine) || wasNewLine)
jsvArrayPush(lines, currentLine);
jsvUnLock(currentLine);
currentLine = 0;
@ -2587,7 +2587,7 @@ JsVar *jswrap_graphics_wrapString(JsVar *parent, JsVar *str, int maxWidth) {
}
jsvStringIteratorFree(&it);
// deal with final line
if (jsvGetStringLength(currentLine)) {
if (!jsvIsEmptyString(currentLine)) {
jsvArrayPush(lines, currentLine);
}
jsvUnLock2(str,currentLine);
@ -3903,7 +3903,7 @@ JsVar *jswrap_graphics_drawImages(JsVar *parent, JsVar *layersVar, JsVar *option
// compose operation
JsVar *opVar = jsvObjectGetChildIfExists(layer,"compose");
layers[i].compose = GFXDILC_REPLACE;
if (!opVar || !jsvGetStringLength(opVar)) layers[i].compose = GFXDILC_REPLACE;
if (!opVar || jsvIsEmptyString(opVar)) layers[i].compose = GFXDILC_REPLACE;
else if (jsvIsStringEqual(opVar,"add")) layers[i].compose = GFXDILC_ADD;
else if (jsvIsStringEqual(opVar,"or")) layers[i].compose = GFXDILC_OR;
else if (jsvIsStringEqual(opVar,"xor")) layers[i].compose = GFXDILC_XOR;

View File

@ -568,7 +568,7 @@ void jswrap_wifi_startAP(
JsVar *jsPassword = jsvObjectGetChildIfExists(jsOptions, "password");
if (jsPassword != NULL) {
// handle password:null
if (jsvGetStringLength(jsPassword) != 0) {
if (!jsvIsEmptyString(jsPassword)) {
if (!jsvIsString(jsPassword) || jsvGetStringLength(jsPassword) < 8) {
jsExceptionHere(JSET_ERROR, "Password must be string of at least 8 characters");
jsvUnLock(jsPassword);

View File

@ -143,11 +143,11 @@ JsVar *jswrap_url_parse(JsVar *url, bool parseQuery) {
JsVar *v;
v = jsvNewWritableStringFromStringVar(url, (size_t)pathStart, JSVAPPENDSTRINGVAR_MAXLENGTH);
if (jsvGetStringLength(v)==0) jsvAppendString(v, "/");
if (jsvIsEmptyString(v)) jsvAppendString(v, "/");
jsvObjectSetChildAndUnLock(obj, "path", v);
v = jsvNewWritableStringFromStringVar(url, (size_t)pathStart, (size_t)((searchStart>=0)?(searchStart-pathStart):JSVAPPENDSTRINGVAR_MAXLENGTH));
if (jsvGetStringLength(v)==0) jsvAppendString(v, "/");
if (jsvIsEmptyString(v)) jsvAppendString(v, "/");
jsvObjectSetChildAndUnLock(obj, "pathname", v);
jsvObjectSetChildAndUnLock(obj, "search", (searchStart>=0)?jsvNewFromStringVar(url, (size_t)searchStart, JSVAPPENDSTRINGVAR_MAXLENGTH):jsvNewNull());
@ -167,7 +167,7 @@ JsVar *jswrap_url_parse(JsVar *url, bool parseQuery) {
while (jsvStringIteratorHasChar(&it)) {
char ch = jsvStringIteratorGetCharAndNext(&it);
if (ch=='&') {
if (jsvGetStringLength(key)>0 || jsvGetStringLength(val)>0) {
if (!jsvIsEmptyString(key) || !jsvIsEmptyString(val)) {
key = jsvAsArrayIndexAndUnLock(key); // make sure "0" gets made into 0
key = jsvMakeIntoVariableName(key, val);
jsvAddName(query, key);
@ -194,7 +194,7 @@ JsVar *jswrap_url_parse(JsVar *url, bool parseQuery) {
jsvStringIteratorFree(&it);
jsvUnLock(queryStr);
if (jsvGetStringLength(key)>0 || jsvGetStringLength(val)>0) {
if (!jsvIsEmptyString(key) || !jsvIsEmptyString(val)) {
key = jsvAsArrayIndexAndUnLock(key); // make sure "0" gets made into 0
key = jsvMakeIntoVariableName(key, val);
jsvAddName(query, key);

View File

@ -696,7 +696,7 @@ bool socketClientConnectionsIdle(JsNetwork *net) {
jsvObjectSetChildAndUnLock(connection, HTTP_NAME_CONNECTED, jsvNewFromBool(true));
alreadyConnected = true;
// if we do not have any data to send, issue a drain event
if (!sendData || (int)jsvGetStringLength(sendData) == 0)
if (!sendData || jsvIsEmptyString(sendData))
jsiQueueObjectCallbacks(connection, HTTP_NAME_ON_DRAIN, &connection, 1);
}
// got data add it to our receive buffer
@ -721,7 +721,7 @@ bool socketClientConnectionsIdle(JsNetwork *net) {
if (!receiveData || jsvIsEmptyString(receiveData)) {
// If we had data to send but the socket closed, this is an error
JsVar *sendData = jsvObjectGetChildIfExists(connection,HTTP_NAME_SEND_DATA);
if (sendData && jsvGetStringLength(sendData) > 0 && error == SOCKET_ERR_CLOSED)
if (sendData && !jsvIsEmptyString(sendData) && error == SOCKET_ERR_CLOSED)
error = SOCKET_ERR_UNSENT_DATA;
jsvUnLock(sendData);

View File

@ -426,7 +426,7 @@ void jswrap_pixljs_init() {
(jshPinGetValue(BTN4_PININDEX) == BTN4_ONSTATE)))
splashScreen = jsfReadFile(jsfNameFromString(".splash"),0,0);
if (jsvIsString(splashScreen)) {
if (jsvGetStringLength(splashScreen)) {
if (!jsvIsEmptyString(splashScreen)) {
graphicsSetVar(&gfx);
jsvUnLock(jswrap_graphics_drawImage(graphics, splashScreen,0,0,0));
graphicsGetFromVar(&gfx, graphics);

View File

@ -1860,7 +1860,7 @@ static void jsiHandleConsoleChar(char ch) {
execInfo.execute &= ~EXEC_CTRL_C_MASK; // if we got Ctrl-C, ignore it
if (inputState == IPS_PACKET_TRANSFER_BYTE0) {
if (jsvGetStringLength(inputLine)==0)
if (jsvIsEmptyString(inputLine))
jsiStatus &= ~JSIS_ECHO_OFF_FOR_LINE; // turn on echo (because it'd have been turned off by DLE on an empty line)
inputPacketLength = ((uint8_t)ch) << 8;
inputState = IPS_PACKET_TRANSFER_BYTE1;
@ -1882,13 +1882,13 @@ static void jsiHandleConsoleChar(char ch) {
} else if (ch == 3) { // Ctrl-c
// Ctrl-C (char code 3) gets handled in an IRQ but we just ignore it here
} else if (ch == 5) { // Ctrl-e
if (jsvGetStringLength(inputLine)==0)
if (jsvIsEmptyString(inputLine))
jsiConsolePrintf("Espruino %s %s\n",JS_VERSION,PC_BOARD_ID); // 5=ENQ - if sent on empty line and Espruino new enough, we transmit what we are
} else if (ch==16) {
/* DLE - Data Link Escape
Espruino uses DLE on the start of a line to signal that just the line in
question should be executed without echo */
if (jsvGetStringLength(inputLine)==0)
if (jsvIsEmptyString(inputLine))
jsiStatus |= JSIS_ECHO_OFF_FOR_LINE;
inputState = IPS_HAD_DLE;
} else if (ch == 27) {

View File

@ -2245,7 +2245,7 @@ void jsvSetInteger(JsVar *v, JsVarInt value) {
*/
bool jsvGetBool(const JsVar *v) {
if (jsvIsString(v))
return jsvGetStringLength((JsVar*)v)!=0;
return !jsvIsEmptyString((JsVar*)v);
#ifndef ESPR_EMBED
if (jsvIsPin(v))
return jshIsPinValid(jshGetPinFromVar((JsVar*)v));

View File

@ -133,7 +133,7 @@ JsVar *jswrap_require(JsVar *moduleName) {
jsvUnLock2(fileContents, exception);
fileContents = 0;
}
if (fileContents && jsvGetStringLength(fileContents)>0)
if (fileContents && !jsvIsEmptyString(fileContents))
moduleExport = jspEvaluateModule(fileContents);
jsvUnLock(fileContents);
}

View File

@ -149,7 +149,7 @@ Prints the contents of the debug log to the console.
*/
void jswrap_ESP8266_printLog() {
JsVar *line = esp8266_logGetLine();
while (jsvGetStringLength(line) > 0) {
while (!jsvIsEmptyString(line)) {
jsiConsolePrintStringVar(line);
jsvUnLock(line);
line = esp8266_logGetLine();