Pages: [1]   Go Down
Author Topic: Arduino 1.0 string changes require changes to examples  (Read 2321 times)
0 Members and 1 Guest are viewing this topic.
Kentwood, MI, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Suddenly, with 1.0, a "sketch" no longer would "verify".

I looked at the documentation http://arduino.cc/en/Reference/StringToUpperCase and found a note that says "As of 1.0, toUpperCase() modifies the string in place rather than returning a new one. "

I thought that an interesting change and took the link to the example at http://arduino.cc/en/Tutorial/StringCaseChanges but found that it DOESN'T WORK with 1.0.

I fingered out what the cryptic line means and that it implied changing, for example
stringTwo = stringTwo.toLowerCase(); to stringTwo.toLowerCase();

Great! I've got it, but I want my code to work on any version during this transition time. I got that fingered out too, and wrote:

#if defined(ARDUINO) && ARDUINO >= 100   // 1.00 or later
    stringTwo.toLowerCase();
  #else
    stringTwo = stringTwo.toLowerCase();      // pre 1.0
  #endif

Now I want to fix the example to show exactly this code, so it will work for everyone. It will also explain the change to everyone and give an example of universal code. When I try, it says "Insufficient privileges to perform action."

How do I become one of the "privileged" few? (one percent?)
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am confused.  Even with the changes to String, shouldn't the old example work?  Maybe it redundantly takes stringTwo and copies/moves it to stringTwo, but this shouldn't keep the example from working.  What is really going on here?

Logged

Kentwood, MI, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, it fails the verify step. While searching for the answer to the new format, I found that this was done on purpose. In this post http://arduino.cc/pipermail/developers_arduino.cc/2011-March/004629.html the developer says:

Quote
Changing trim(), replace(), toUpperCase(), and toLowerCase() to not
return the string after they modify it.  This makes it clearer that
they operate on the string they're called on, rather than returning a
new string and leaving the original unchanged. It means that prior
code that used these functions will get a compiler error, which I
prefer to silently changing the behavior of a program.
(bold added)

This is the error it produces:

 cpp: In function 'void loop()':
 24: error: no match for 'operator=' in 'stringOne = stringOne.String::toUpperCase()'
C:\...\arduino/WString.h:83: note: candidates are: String& String::operator=(const String&)
C:\...\arduino/WString.h:84: note:                 String& String::operator=(const char*)
« Last Edit: February 24, 2012, 01:25:29 am by JEDtoo » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh gods of Arduino on high hear my plea! Please change the toLowerCase and the toUpperCase  examples to reflect the change in syntax. 
Ver 1.0 syntax is:
stringOne.toLowerCase();
stringOne.toUpperCase();
not
stringOne  =  stringOne.toUpperCase();
Wasted my morning chasing this down. Thank you. 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The example for trim also needs changed:
Syntax in example
Is:
stringOne = stringOne.trim();
Should be:
stringOne.trim();
Logged

0
Offline Offline
God Member
*****
Karma: 26
Posts: 626
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm the guy who rewrote String, to fix the many bugs it had in 0019 to 0023.  I make the Teensy boards.  When I put String into Teensyduino, people were having all sorts of problems.  It was incredibly buggy.  I put a LOT of work into fixing the many problems, and carefully analyzing and optimizing memory allocation patterns.

The String I contributed had trim(), toUpperCase() and toLowerCase() return a reference to the original string, so these very common usage cases would be backwards compatible.  In fact, the version of String currently in Teensyduino still has these, so the examples work properly on Teensy boards.  smiley

Sadly, many changes were made to my code.  Much of the careful work I did, especially on optimizing memory allocation, just doesn't apply on Arduino's version.  Many usages of String that should work simply do not on Arduino's version.  Some even crash.  There's nothing I can do now about Arduino.  I tried very hard to make String work as well as possible (and it does on Teensy), and I tried to share that with all Arduino users.  I really wish it had turned out better.
« Last Edit: May 07, 2012, 06:26:19 am by Paul Stoffregen » Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 215
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't feel bad Paul. For years I was a VB programmer... then after VB6 good old MF... excuse me I meant MS ...Freudian slip. Good old MS decided that VB was not sophisticated enough so they ripped the shit out of it and made it into a lame Java or C# clone. I tried to re-compile a major app I was working on and got 670 error messages! Needles to say I did not and have not migrated.

Anyway you tried... Thanks
Logged

Pages: [1]   Go Up
Jump to: