Using the functions in WString.cpp I have run into a problem with .substring usage. The function does not work correctly.
With:
String sInString = "P255"
String sOutString;
sOutString = sInString.substring(1,3); // should return "255", actually returns "25"
sOutString = sInString.substring(1,4); // returns "255" which is what the first example should return.
Looking at the source, the last character is being overwritten with '\0' according to the docs, and all other instances of gcc libraries seem to work correctly. Has this been corrected in any version of the arduino libraries for esp32. I am using the aurduino/esp32 implementation installed with the latest version of VisualMicro.
Source in the library is:
tring String::substring(unsigned int left, unsigned int right) const {
if(left > right) {
unsigned int temp = right;
right = left;
left = temp;
}
String out;
if(left >= len())
return out;
if(right > len())
right = len();
char temp = buffer()[right]; // save the replaced character
wbuffer()[right] = '\0';
out = wbuffer() + left; // pointer arithmetic
wbuffer()[right] = temp; //restore character
return out;
}
which is completely different from what is in the core libraries in the master branch on github.
Am I missing something......
WString.cpp
-
- Posts: 828
- Joined: Mon Jul 22, 2019 3:20 pm
Re: WString.cpp
It comes from Arduino.
https://www.arduino.cc/en/Tutorial/StringSubstring
https://www.arduino.cc/reference/en/lan ... substring/
If you want to capture from the first character, you are counting over 0 characters, and then selecting (right - left) characters.
https://www.arduino.cc/en/Tutorial/StringSubstring
https://www.arduino.cc/reference/en/lan ... substring/
If you want to capture from the first character, you are counting over 0 characters, and then selecting (right - left) characters.
Who is online
Users browsing this forum: No registered users and 81 guests