Strange behavior when using strings read from files

Posted on
  • I have done some experimenting with numbers read from a file and found the following two problems:

    require("Storage").write("test","teststr­ing");
    require("Storage").read("test");
    let newString = require("Storage").read("test");
    newString += "other";
    // "other" is missing from string
    print(newString);
    
    require("Storage").write("test","123");
    require("Storage").read("test");
    let newString = require("Storage").read("test");
    newString += 123;
    // internal error, same if appending string to number read from file
    print(newString);
    

    I imagine both of these are caused by the file based strings mapped directly to the file instead of living in RAM as the other strings do.
    This behaviour can be worked around by prepending an empty string when reading:

    newString = "" + require("Storage").read("test");
    

    Does this mean, that the whole resulting string is in RAM? Not that bad for my usecase, since I am reading a few bytes at a time from a bigger file, but could be relevant when reading whole files.

  • what replacing += bit by newString = newString + "other"; would do?
    If that one works I guess it is a bug that += is not working the same.

    And yes adding anything makes a copy of whole string.

  • That's interesting... I'll file an issue on this (https://github.com/espruino/Espruino/iss­ues/2268) and will look into it when I have a second - it's possible that += is unaware that it can't append to a string in flash memory.

    Does this mean, that the whole resulting string is in RAM?

    Yes, if you perform an operation on the string the result is in RAM...

  • newString = newString + "other"; shows the same behaviour. In that case prepending an empty string also works. The problem seems to hinge on the flash string beeing the first in the expression.
    Actually newString = newString.concat("other"); does not work either, as well as newString = newString.substring() + "other";

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Strange behavior when using strings read from files

Posted by Avatar for halemmerich @halemmerich

Actions