1
1
/*
2
- Copyright (c) 2014 Arduino. All right reserved.
2
+ WString.cpp - String library for Wiring & Arduino
3
+ ...mostly rewritten by Paul Stoffregen...
4
+ Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
5
+ Copyright 2011, Paul Stoffregen, [email protected]
3
6
4
7
This library is free software; you can redistribute it and/or
5
8
modify it under the terms of the GNU Lesser General Public
8
11
9
12
This library is distributed in the hope that it will be useful,
10
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
- See the GNU Lesser General Public License for more details.
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ Lesser General Public License for more details.
13
16
14
17
You should have received a copy of the GNU Lesser General Public
15
18
License along with this library; if not, write to the Free Software
@@ -213,10 +216,10 @@ void String::move(String &rhs)
213
216
String & String::operator = (const String &rhs)
214
217
{
215
218
if (this == &rhs) return *this ;
216
-
219
+
217
220
if (rhs.buffer ) copy (rhs.buffer , rhs.len );
218
221
else invalidate ();
219
-
222
+
220
223
return *this ;
221
224
}
222
225
@@ -238,7 +241,7 @@ String & String::operator = (const char *cstr)
238
241
{
239
242
if (cstr) copy (cstr, strlen (cstr));
240
243
else invalidate ();
241
-
244
+
242
245
return *this ;
243
246
}
244
247
@@ -481,7 +484,7 @@ unsigned char String::equalsIgnoreCase( const String &s2 ) const
481
484
const char *p2 = s2.buffer ;
482
485
while (*p1) {
483
486
if (tolower (*p1++) != tolower (*p2++)) return 0 ;
484
- }
487
+ }
485
488
return 1 ;
486
489
}
487
490
@@ -512,7 +515,7 @@ char String::charAt(unsigned int loc) const
512
515
return operator [](loc);
513
516
}
514
517
515
- void String::setCharAt (unsigned int loc, char c)
518
+ void String::setCharAt (unsigned int loc, char c)
516
519
{
517
520
if (loc < len) buffer[loc] = c;
518
521
}
@@ -618,10 +621,10 @@ String String::substring(unsigned int left, unsigned int right) const
618
621
left = temp;
619
622
}
620
623
String out;
621
- if (left > len) return out;
624
+ if (left >= len) return out;
622
625
if (right > len) right = len;
623
626
char temp = buffer[right]; // save the replaced character
624
- buffer[right] = ' \0 ' ;
627
+ buffer[right] = ' \0 ' ;
625
628
out = buffer + left; // pointer arithmetic
626
629
buffer[right] = temp; // restore character
627
630
return out;
@@ -683,15 +686,16 @@ void String::replace(const String& find, const String& replace)
683
686
}
684
687
685
688
void String::remove (unsigned int index){
686
- if (index >= len) { return ; }
687
- int count = len - index;
688
- remove (index, count);
689
+ // Pass the biggest integer as the count. The remove method
690
+ // below will take care of truncating it at the end of the
691
+ // string.
692
+ remove (index, (unsigned int )-1 );
689
693
}
690
694
691
695
void String::remove (unsigned int index, unsigned int count){
692
696
if (index >= len) { return ; }
693
697
if (count <= 0 ) { return ; }
694
- if (index + count > len) { count = len - index; }
698
+ if (count > len - index ) { count = len - index; }
695
699
char *writeTo = buffer + index;
696
700
len = len - count;
697
701
strncpy (writeTo, buffer + index + count,len - index);
0 commit comments