Pages: [1]   Go Down
Author Topic: String library bug? startsWith always returns true  (Read 1021 times)
0 Members and 1 Guest are viewing this topic.
Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

apparently I've run into a bug in regards to the String library.
The odd part is, I've used the startsWith method before, and it worked a treat.
But whenever I try to use it now, it always returns true.

I tried it with the old v0.4 library, the v0.8 library (both the one I had, and a fresh download), but it keeps returning true.
Won't include the library in this post, as I've been using an unmodified version of the one found on this site.

Sketch used for testing purposes:
Code:
#include <WString.h>

void setup()
{
  Serial.begin(9600);
  
  String test = "foo";
  
  if(test.startsWith("f")) { Serial.println("starts with f"); }
  if(test.startsWith("bar")) { Serial.println("starts with bar"); }
  if(test.startsWith("far too large a string")) { Serial.println("I'm stumped"); }
}

void loop()
{
}

I use ubuntu 9.10, and I have a feeling it has something to do with an update or so (the usual culprit when something stops working for no apparent reason), but it remains quite annoying.
The arduino I'm using is a DFRduino duemilanove with Atmega328 chip.

Anybody have an idea for a work-around, fix or cause to this bug?
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ran your example on Ubuntu 9.10 (with Arduino IDE version 18, String library version 0.8 just downloaded) and I got the same results you did.

I then ran it under Windows/XP (with Arduino IDE version 17, also String library version 0.smiley-cool and got the same results there.

I deleted all the *string*.o files I could find, re-compiled and re-uploaded.

I ran both tests with a stock Arduino Duemilanove w/Atmega328, genuine "Made in Italy".

I tried some of the examples that are distributed with the library, and none of the methods seem to work under either environment.

The setup() code from the StringEndsWith.pde example (modified to highlight the version):
Code:
#include <WString.h>                // include the String library

#define maxLength 30

String inString = String(maxLength);       // allocate a new String

void setup() {
  // open the serial port:
  Serial.begin(9600);
  // Say hello:
  Serial.print("String Library version: ->");
  Serial.print(inString.version());
  Serial.println("<-");
}

void loop() {
}

prints:

Code:
String Library version: -><-

Can anyone else run these simple examples, whatever your environment is?
« Last Edit: April 13, 2010, 01:30:15 am by TBAr » Logged

Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And just as suddenly as this bug appeared, it vanishes?
I copy-pasted the old v0.4 library back in my 018 folder, and suddenly it works again (was going to give it another round of debugging).
 :o

This makes me wonder, this would imply that the error isn't in the code..
Could this bug be related to the IDE? like the 'my program disappeared!' bug mentioned here?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is becouse the string library contains a bug in the constructor. For example:

Code:
String test = "Foo";
Serial.println(test.length); // Prints 0
Serial.println(test.cstr()); // Prints foo


It can be fixed by changing String.cpp as follows:

Code:
String::String(const char* bytes)
{  
  if(bytes == NULL)
    bytes= "";
  
  _length = strlen(bytes);
  _capacity = _length+1;
  _array = (char*)malloc(_capacity);
    
  //clear();
  setArray(bytes);
}

the call to clear sets the length of the string to 0.
« Last Edit: May 18, 2010, 07:17:06 am by abstractor » Logged

Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
String::String(const char* bytes)
{
  if(bytes == NULL)
    bytes= "";
  _length = strlen(bytes);
  //if (_capacity < _length) {
  _capacity = _length;
  //  free(_array);
  _array = (char*)malloc(_length+1);
  //}
    
  clear();  
  setArray(bytes);
}

The constructor in my version appears to be fine.
The clear() method sets the entire array to null characters, it doesn't reset the length.
So it resets the array, then writes the argument to the array.
Appears to be solid code  :-/
Still no idea what caused this bug, it disappeared as randomly as it appeared.
Logged

New York, NY, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Howdy. I don't check PMs often, so email me!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the discoveries, and sorry for my delay in replying to this.  We've never included String in the core libraries partially because it's always been a little buggy and not ready for prime time.  One of my colleagues and I are doing some work to improve that in the coming weeks, though, and I hope we'll have a new version up soon.
Logged

New York, NY, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Howdy. I don't check PMs often, so email me!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

...and the length bugfixes are already in 0.9, posted now.
Logged

Pages: [1]   Go Up
Jump to: