Commit Graph

47 Commits

Author SHA1 Message Date
Sandeep Mistry 5dd628a2c2 Make String::move of an invalidated String result in an invalidated String 2016-07-12 17:29:13 -04:00
Matthijs Kooijman 72a5ef3c78 Check for __cplusplus >= 201103L as well as __GXX_EXPERIMENTAL_CXX0X__
Gcc 4.8 defines __cplusplus as 201103L, so we can check for that now. It
still also defines __GXX_EXPERIMENTAL_CXX0X__, but this could help on
other compilers, or if gcc ever decides to stop defining the
experimental macro.
2015-07-16 13:06:10 +02:00
Matthijs Kooijman fde95cf5b5 Fix off-by-one in String::substring
When checking the `left` argument, it previously allowed having
left == len. However, this means the substring starts one past the last
character in the string and should return the empty string. In practice,
this already worked correctly, because buffer[len] contains the trailing
nul, so it would (re)assign the empty string to `out`.

However, fixing this check makes it a bit more logical, and prevents a
fairly unlikely out-of-buffer write (to address 0x0) when calling
substring on an invalidated String:

	String bar = (char*)NULL;
	bar.substring(0, 0);
2014-09-10 13:42:06 +02:00
Matthijs Kooijman 35a84769d4 Simplify String::remove(unsigned int)
Previously, this method calculated the length of the string from the
given index onwards. However, the other remove() method called already
contains code for this calculation, which is used when the count passed
in is too big. This means we can just pass in a very big count that is
guaranteed to point past the end of the string, shrinking the remove
method by a few bytes.
2014-09-10 12:33:25 +02:00
Matthijs Kooijman b2729a5156 Fix bounds check in String::remove()
Previously, if you passed in a very big index and/or count, the
`index + count` could overflow, making the count be used as-is instead
of being truncated (causing the string to be updated wrongly and
potentially writing to arbitrary memory locations).

We can rewrite the comparison to use `len - index` instead. Since we
know that index < len, we are sure this subtraction does not overflow,
regardless of what values of index and count we pass in.

As an added bonus, the `len - index` value already needed be calculated
inside the if, so this saves a few instructions in the generated code.

To illustrate this problem, consider this code:

String foo = "foo";
Serial.println(foo.length()); // Prints 3
foo.remove(1, 65535); // Should remove all but first character
Serial.println(foo.length()); // Prints 4 without this patch

Not shown in this is example is that some arbitrary memory is written
as well.
2014-09-10 12:33:25 +02:00
Matthijs Kooijman dfb0dee773 Remove unneeded check in String::remove(unsigned int)
This check already happens in the remove(unsigned int, unsigned int)
method that is caled, so there is no need to also check this here.
2014-09-10 12:33:24 +02:00
Cristian Maglie 1eee97d980 Improved portability of String class (maniacbug) 2014-01-01 17:22:40 +01:00
Matthijs Kooijman 1978e82e4e Use PGM_P instead of prog_char
On later versions of avr-libc, prog_char is deprecated. In 0acebeeff48
the one occurence of prog_char was replaced by "char PROGMEM", which is
not entirely correct (PROGMEM is supposed to be an attribute on a
variable, not on a type, even though this is how things work in older
libc versions). However, in 1130fede3a2 a few new occurences of
prog_char are introduced, which break compilation on newer libc versions
again.

This commit changes all these pointer types to use the PGM_P macro from
<avr/pgmspace.h>. This macro is just "const char *" in newer libc
versions and "const prog_char *" in older versions, so it should always
work.

References #795
2013-12-31 20:01:40 +01:00
Cristian Maglie cbeaa543fc Fixed String class regression after f80c6c5f35cddcf4761a3c97feb8504425e9d27d
This should make explicit String-from-integer constructor working again:

   int a = 10;
   String(a, 4);
2013-09-03 18:40:30 +02:00
Cristian Maglie b3348a6706 Removed unused flags from String (free 1 byte of SRAM) 2013-08-20 15:15:47 +02:00
Cristian Maglie 620fe0a3ac String: fixed number of whitespaces in concat() methods 2013-06-28 09:53:25 +02:00
Cristian Maglie a7ba61d1b7 String: changed default to 2 decimal digits 2013-06-21 21:23:12 +02:00
Cristian Maglie f25e5e94f7 Fixed buffer overflow on String class (Paul Stoffregen) 2013-06-06 20:04:43 +02:00
Cristian Maglie 550b6adcfc Merged various bugfix / improvements to String class.
Merge branch 'master' into ide-1.5.x
2013-06-06 19:54:58 +02:00
Cristian Maglie db286ac0c1 Added support for Flash string on String class. 2013-06-06 16:33:20 +02:00
Cristian Maglie 2719777a48 String class: removed deep copy on substring method.
Small code cleanup.
2013-06-06 16:33:20 +02:00
Tevin Zhang c8a79d0d0c add String.toFloat 2013-06-06 16:19:34 +02:00
Ryan Esteves 6bef2ada06 Added remove methods to WString 2013-06-05 14:08:59 -04:00
David A. Mellis 95b51c7728 Fixing warnings (unsigned comparisons to 0). (maniacbug) 2012-01-02 12:57:23 -05:00
David A. Mellis 5c9d10ad94 Bug fix in replace().
http://code.google.com/p/arduino/issues/detail?id=694
2011-10-25 11:15:14 -04:00
David A. Mellis 77cdeb5b93 Fixing more warnings (Paul Stoffregen). 2011-10-10 11:28:44 -04:00
David A. Mellis 1cac0f3eb7 Restoring concatenation of built-in types with String. 2011-03-31 10:56:14 -04:00
David A. Mellis 99e642a26d String: removing implicit numeric conversions and new approach to "if (s)".
This makes explicit the String constructors that take numeric types and chars and removes the versions of concat() and operator=() and operator+() that accept numberic types.

It also replaces the operator bool() with a operator that converts to a function pointer.  This allows for uses like "if (s)" but not "s + 123".  See: http://www.artima.com/cppsource/safebool.html.  This allowed removing the disambiguating operator+() functions and relying solely on StringSumHelper and anonymous temporaries once again.

Also, now treating unsigned char's like int when constructing Strings from them, i.e. String(byte(65)) is now "65" not "A".  This is consistent with the new behavior of Serial.print(byte).
2011-03-26 18:52:54 -04:00
David A. Mellis 58d04ab3a3 Return an invalid string (not a partial one) when operator+() fails. 2011-03-19 11:14:17 -04:00
David A. Mellis 7dea0522f4 Starting to distinguish between empty strings and invalid (null) ones. 2011-03-18 21:45:27 -04:00
David A. Mellis 76776e7a46 Moving move() to __GXX_EXPERIMENTAL_CXX0X__ only, adding operator bool(). 2011-03-13 16:46:06 -04:00
David A. Mellis 98b403114c Modifying String.concat() to return success or failure, not this.
Which means you can't chain multiple concat() calls together, but you can check if they succeeded or not.
2011-03-12 14:03:34 -05:00
David A. Mellis b4b32f60f1 Don't return the string when modifying its value.
Changing toLowerCase(), toUpperCase(), trim() and replace() to return void instead of a reference to the string that's just been changed.  That way, it's clear that the functions modify the string they've been called on.
2011-03-11 18:54:58 -05:00
David A. Mellis 99e0c131fc Renaming append() back to concat(). 2011-03-11 18:04:31 -05:00
David A. Mellis 22786eaed2 Removing F("string") syntax for now.
We should probably add something like this back in later, but I want to do one thing at a time.  This removes the __FlashStringHelper class as well.
2011-03-11 18:01:40 -05:00
David A. Mellis 438bca3cb2 Rewrite of the String class by Paul Stoffregen.
http://www.pjrc.com/teensy/string_class_experimental.html
2011-03-11 17:56:10 -05:00
David A. Mellis e009c5c6c6 Renamed WProgram.h to Arduino.h. 2011-03-01 19:52:13 -05:00
David A. Mellis 11dd06436d Changing String append to use realloc(); thanks to Paul Stoffregen.
http://code.google.com/p/arduino/issues/detail?id=332
2010-12-11 15:22:07 -05:00
David A. Mellis 053ec1b989 Replacing custom String.toInt() function with a call to atol(). 2010-12-03 23:12:41 -05:00
David A. Mellis 4a90c4bd40 Redoing 448222e4b65e0cf44dfc0c494f7f76901f1fabea without all the extra files.
Adds toInt() to String, WCharacter.h (from Wiring), and an SD Datalogger example.
2010-11-29 15:20:30 -05:00
David A. Mellis 80c5173bfd Revert "added toInt() function to WString".
This reverts commit 448222e4b65e0cf44dfc0c494f7f76901f1fabea.
2010-11-29 15:14:10 -05:00
Tom Igoe 15f51fc1f8 added toInt() function to WString 2010-11-29 11:31:00 -05:00
David A. Mellis 4bbd4f7448 Changing String::toCharArray() and getBytes() to accept a buffer, rather than return one. That way they don't expose the internal representation of the String class, allowing future optimization. Thanks to Paul Stoffregen. 2010-08-28 10:23:54 +00:00
David A. Mellis 1362ca26c1 Returning a reference to a dummy character for indices beyond the string length (in operator[]). 2010-08-28 09:55:26 +00:00
David A. Mellis 14831247bc Adding some basic error checking to the String class (i.e. checking for a non-null buffer before modifying its contents). 2010-08-18 21:39:28 +00:00
David A. Mellis e871ae5236 Reverting changes to String class and modifying to allow + and += to work on more types. 2010-07-27 14:20:56 +00:00
Tom Igoe 8eea4576c4 removed append() from String library 2010-07-27 10:18:04 +00:00
Tom Igoe 58f5b67b71 checked in Xiaoyang Feng's changes to String library 2010-07-27 10:09:09 +00:00
Tom Igoe 445d1688c9 Checked in Xiaoyang's changes to String library 2010-07-27 10:06:43 +00:00
David A. Mellis 9f1e60ffa0 whitespace. 2010-07-05 16:08:35 +00:00
David A. Mellis 50bfce889e Modifying String from new/delete to malloc()/free(). Also #include'ing WString.h from WProgram.h. 2010-07-04 16:36:52 +00:00
David A. Mellis 0c09741c72 Adding WString.h and WString.cpp straight from Wiring (r. 726). Won't actually work yet. 2010-07-04 16:27:23 +00:00