From 378df0359ce786b9e2870349083f31891d816cbf Mon Sep 17 00:00:00 2001 From: Christopher Andrews Date: Sun, 13 Jul 2014 22:55:53 +1000 Subject: [PATCH 1/2] Updated String library to use C++11 iterators. This will allow using the String library in a ranged for loop: ```C++ String s = "Hi, this is a test"; for( char c : s ) Serial.print( c ); ``` --- hardware/arduino/avr/cores/arduino/WString.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index 74024309278..b91c2426695 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -89,6 +89,8 @@ class String #ifdef __GXX_EXPERIMENTAL_CXX0X__ String & operator = (String &&rval); String & operator = (StringSumHelper &&rval); + auto begin() -> const char* { return c_str(); } + auto end() -> const char* { return c_str() + length(); } #endif // concatenate (works w/ built-in types) From fe6ba9ba7b8dee9d3e118bfcc9c89c6f4f8c8ba2 Mon Sep 17 00:00:00 2001 From: Christopher Andrews Date: Tue, 15 Jul 2014 16:19:41 +1000 Subject: [PATCH 2/2] Removed C++11 dependency for `begin()` and `end()` As I was not able to base the return types of `begin()` & `end()` off the c_str() function, I have changed the source so the features can be used by C++98 code, while still allowing ranged loops in C++11. --- hardware/arduino/avr/cores/arduino/WString.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/WString.h b/hardware/arduino/avr/cores/arduino/WString.h index b91c2426695..9b27530e6a4 100644 --- a/hardware/arduino/avr/cores/arduino/WString.h +++ b/hardware/arduino/avr/cores/arduino/WString.h @@ -89,8 +89,6 @@ class String #ifdef __GXX_EXPERIMENTAL_CXX0X__ String & operator = (String &&rval); String & operator = (StringSumHelper &&rval); - auto begin() -> const char* { return c_str(); } - auto end() -> const char* { return c_str() + length(); } #endif // concatenate (works w/ built-in types) @@ -163,6 +161,8 @@ class String void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const {getBytes((unsigned char *)buf, bufsize, index);} const char * c_str() const { return buffer; } + const char* begin() { return c_str(); } + const char* end() { return c_str() + length(); } // search int indexOf( char ch ) const;