My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

WString.cpp 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. WString.cpp - String library for Wiring & Arduino
  3. Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include <stdlib.h>
  17. #include "WProgram.h"
  18. #include "WString.h"
  19. String::String( const char *value )
  20. {
  21. if ( value == NULL )
  22. value = "";
  23. getBuffer( _length = strlen( value ) );
  24. if ( _buffer != NULL )
  25. strcpy( _buffer, value );
  26. }
  27. String::String( const String &value )
  28. {
  29. getBuffer( _length = value._length );
  30. if ( _buffer != NULL )
  31. strcpy( _buffer, value._buffer );
  32. }
  33. String::String( const char value )
  34. {
  35. _length = 1;
  36. getBuffer(1);
  37. if ( _buffer != NULL ) {
  38. _buffer[0] = value;
  39. _buffer[1] = 0;
  40. }
  41. }
  42. String::String( const unsigned char value )
  43. {
  44. _length = 1;
  45. getBuffer(1);
  46. if ( _buffer != NULL) {
  47. _buffer[0] = value;
  48. _buffer[1] = 0;
  49. }
  50. }
  51. String::String( const int value, const int base )
  52. {
  53. char buf[33];
  54. itoa((signed long)value, buf, base);
  55. getBuffer( _length = strlen(buf) );
  56. if ( _buffer != NULL )
  57. strcpy( _buffer, buf );
  58. }
  59. String::String( const unsigned int value, const int base )
  60. {
  61. char buf[33];
  62. ultoa((unsigned long)value, buf, base);
  63. getBuffer( _length = strlen(buf) );
  64. if ( _buffer != NULL )
  65. strcpy( _buffer, buf );
  66. }
  67. String::String( const long value, const int base )
  68. {
  69. char buf[33];
  70. ltoa(value, buf, base);
  71. getBuffer( _length = strlen(buf) );
  72. if ( _buffer != NULL )
  73. strcpy( _buffer, buf );
  74. }
  75. String::String( const unsigned long value, const int base )
  76. {
  77. char buf[33];
  78. ultoa(value, buf, 10);
  79. getBuffer( _length = strlen(buf) );
  80. if ( _buffer != NULL )
  81. strcpy( _buffer, buf );
  82. }
  83. char String::charAt( unsigned int loc ) const
  84. {
  85. return operator[]( loc );
  86. }
  87. void String::setCharAt( unsigned int loc, const char aChar )
  88. {
  89. if(_buffer == NULL) return;
  90. if(_length > loc) {
  91. _buffer[loc] = aChar;
  92. }
  93. }
  94. int String::compareTo( const String &s2 ) const
  95. {
  96. return strcmp( _buffer, s2._buffer );
  97. }
  98. const String & String::concat( const String &s2 )
  99. {
  100. return (*this) += s2;
  101. }
  102. const String & String::operator=( const String &rhs )
  103. {
  104. if ( this == &rhs )
  105. return *this;
  106. if ( rhs._length > _length )
  107. {
  108. free(_buffer);
  109. getBuffer( rhs._length );
  110. }
  111. if ( _buffer != NULL ) {
  112. _length = rhs._length;
  113. strcpy( _buffer, rhs._buffer );
  114. }
  115. return *this;
  116. }
  117. //const String & String::operator+=( const char aChar )
  118. //{
  119. // if ( _length == _capacity )
  120. // doubleBuffer();
  121. //
  122. // _buffer[ _length++ ] = aChar;
  123. // _buffer[ _length ] = '\0';
  124. // return *this;
  125. //}
  126. const String & String::operator+=( const String &other )
  127. {
  128. _length += other._length;
  129. if ( _length > _capacity )
  130. {
  131. char *temp = (char *)realloc(_buffer, _length + 1);
  132. if ( temp != NULL ) {
  133. _buffer = temp;
  134. _capacity = _length;
  135. } else {
  136. _length -= other._length;
  137. return *this;
  138. }
  139. }
  140. strcat( _buffer, other._buffer );
  141. return *this;
  142. }
  143. int String::operator==( const String &rhs ) const
  144. {
  145. return ( _length == rhs._length && strcmp( _buffer, rhs._buffer ) == 0 );
  146. }
  147. int String::operator!=( const String &rhs ) const
  148. {
  149. return ( _length != rhs.length() || strcmp( _buffer, rhs._buffer ) != 0 );
  150. }
  151. int String::operator<( const String &rhs ) const
  152. {
  153. return strcmp( _buffer, rhs._buffer ) < 0;
  154. }
  155. int String::operator>( const String &rhs ) const
  156. {
  157. return strcmp( _buffer, rhs._buffer ) > 0;
  158. }
  159. int String::operator<=( const String &rhs ) const
  160. {
  161. return strcmp( _buffer, rhs._buffer ) <= 0;
  162. }
  163. int String::operator>=( const String & rhs ) const
  164. {
  165. return strcmp( _buffer, rhs._buffer ) >= 0;
  166. }
  167. char & String::operator[]( unsigned int index )
  168. {
  169. static char dummy_writable_char;
  170. if (index >= _length || !_buffer) {
  171. dummy_writable_char = 0;
  172. return dummy_writable_char;
  173. }
  174. return _buffer[ index ];
  175. }
  176. char String::operator[]( unsigned int index ) const
  177. {
  178. // need to check for valid index, to do later
  179. return _buffer[ index ];
  180. }
  181. boolean String::endsWith( const String &s2 ) const
  182. {
  183. if ( _length < s2._length )
  184. return 0;
  185. return strcmp( &_buffer[ _length - s2._length], s2._buffer ) == 0;
  186. }
  187. boolean String::equals( const String &s2 ) const
  188. {
  189. return ( _length == s2._length && strcmp( _buffer,s2._buffer ) == 0 );
  190. }
  191. boolean String::equalsIgnoreCase( const String &s2 ) const
  192. {
  193. if ( this == &s2 )
  194. return true; //1;
  195. else if ( _length != s2._length )
  196. return false; //0;
  197. return strcmp(toLowerCase()._buffer, s2.toLowerCase()._buffer) == 0;
  198. }
  199. String String::replace( char findChar, char replaceChar )
  200. {
  201. if ( _buffer == NULL ) return *this;
  202. String theReturn = _buffer;
  203. char* temp = theReturn._buffer;
  204. while( (temp = strchr( temp, findChar )) != 0 )
  205. *temp = replaceChar;
  206. return theReturn;
  207. }
  208. String String::replace( const String& match, const String& replace )
  209. {
  210. if ( _buffer == NULL ) return *this;
  211. String temp = _buffer, newString;
  212. int loc;
  213. while ( (loc = temp.indexOf( match )) != -1 )
  214. {
  215. newString += temp.substring( 0, loc );
  216. newString += replace;
  217. temp = temp.substring( loc + match._length );
  218. }
  219. newString += temp;
  220. return newString;
  221. }
  222. int String::indexOf( char temp ) const
  223. {
  224. return indexOf( temp, 0 );
  225. }
  226. int String::indexOf( char ch, unsigned int fromIndex ) const
  227. {
  228. if ( fromIndex >= _length )
  229. return -1;
  230. const char* temp = strchr( &_buffer[fromIndex], ch );
  231. if ( temp == NULL )
  232. return -1;
  233. return temp - _buffer;
  234. }
  235. int String::indexOf( const String &s2 ) const
  236. {
  237. return indexOf( s2, 0 );
  238. }
  239. int String::indexOf( const String &s2, unsigned int fromIndex ) const
  240. {
  241. if ( fromIndex >= _length )
  242. return -1;
  243. const char *theFind = strstr( &_buffer[ fromIndex ], s2._buffer );
  244. if ( theFind == NULL )
  245. return -1;
  246. return theFind - _buffer; // pointer subtraction
  247. }
  248. int String::lastIndexOf( char theChar ) const
  249. {
  250. return lastIndexOf( theChar, _length - 1 );
  251. }
  252. int String::lastIndexOf( char ch, unsigned int fromIndex ) const
  253. {
  254. if ( fromIndex >= _length )
  255. return -1;
  256. char tempchar = _buffer[fromIndex + 1];
  257. _buffer[fromIndex + 1] = '\0';
  258. char* temp = strrchr( _buffer, ch );
  259. _buffer[fromIndex + 1] = tempchar;
  260. if ( temp == NULL )
  261. return -1;
  262. return temp - _buffer;
  263. }
  264. int String::lastIndexOf( const String &s2 ) const
  265. {
  266. return lastIndexOf( s2, _length - s2._length );
  267. }
  268. int String::lastIndexOf( const String &s2, unsigned int fromIndex ) const
  269. {
  270. // check for empty strings
  271. if ( s2._length == 0 || s2._length - 1 > fromIndex || fromIndex >= _length )
  272. return -1;
  273. // matching first character
  274. char temp = s2[ 0 ];
  275. for ( int i = fromIndex; i >= 0; i-- )
  276. {
  277. if ( _buffer[ i ] == temp && (*this).substring( i, i + s2._length ).equals( s2 ) )
  278. return i;
  279. }
  280. return -1;
  281. }
  282. boolean String::startsWith( const String &s2 ) const
  283. {
  284. if ( _length < s2._length )
  285. return 0;
  286. return startsWith( s2, 0 );
  287. }
  288. boolean String::startsWith( const String &s2, unsigned int offset ) const
  289. {
  290. if ( offset > _length - s2._length )
  291. return 0;
  292. return strncmp( &_buffer[offset], s2._buffer, s2._length ) == 0;
  293. }
  294. String String::substring( unsigned int left ) const
  295. {
  296. return substring( left, _length );
  297. }
  298. String String::substring( unsigned int left, unsigned int right ) const
  299. {
  300. if ( left > right )
  301. {
  302. int temp = right;
  303. right = left;
  304. left = temp;
  305. }
  306. if ( right > _length )
  307. {
  308. right = _length;
  309. }
  310. char temp = _buffer[ right ]; // save the replaced character
  311. _buffer[ right ] = '\0';
  312. String outPut = ( _buffer + left ); // pointer arithmetic
  313. _buffer[ right ] = temp; //restore character
  314. return outPut;
  315. }
  316. String String::toLowerCase() const
  317. {
  318. String temp = _buffer;
  319. for ( unsigned int i = 0; i < _length; i++ )
  320. temp._buffer[ i ] = (char)tolower( temp._buffer[ i ] );
  321. return temp;
  322. }
  323. String String::toUpperCase() const
  324. {
  325. String temp = _buffer;
  326. for ( unsigned int i = 0; i < _length; i++ )
  327. temp._buffer[ i ] = (char)toupper( temp._buffer[ i ] );
  328. return temp;
  329. }
  330. String String::trim() const
  331. {
  332. if ( _buffer == NULL ) return *this;
  333. String temp = _buffer;
  334. unsigned int i,j;
  335. for ( i = 0; i < _length; i++ )
  336. {
  337. if ( !isspace(_buffer[i]) )
  338. break;
  339. }
  340. for ( j = temp._length - 1; j > i; j-- )
  341. {
  342. if ( !isspace(_buffer[j]) )
  343. break;
  344. }
  345. return temp.substring( i, j + 1);
  346. }
  347. void String::getBytes(unsigned char *buf, unsigned int bufsize)
  348. {
  349. if (!bufsize || !buf) return;
  350. unsigned int len = bufsize - 1;
  351. if (len > _length) len = _length;
  352. strncpy((char *)buf, _buffer, len);
  353. buf[len] = 0;
  354. }
  355. void String::toCharArray(char *buf, unsigned int bufsize)
  356. {
  357. if (!bufsize || !buf) return;
  358. unsigned int len = bufsize - 1;
  359. if (len > _length) len = _length;
  360. strncpy(buf, _buffer, len);
  361. buf[len] = 0;
  362. }
  363. long String::toInt() {
  364. return atol(_buffer);
  365. }