Microduino

Dependencies:   mbed

Fork of Io_moon by Li Weiyi

Committer:
lixianyu
Date:
Thu Jun 23 11:16:14 2016 +0000
Revision:
0:740c1eb2df13
* AM2321?????????2s????i2c?????; * SimpleTimer??bug?????????????????????????; * Blynk??bug??????????????; * ?????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:740c1eb2df13 1 /*
lixianyu 0:740c1eb2df13 2 WString.cpp - String library for Wiring & Arduino
lixianyu 0:740c1eb2df13 3 ...mostly rewritten by Paul Stoffregen...
lixianyu 0:740c1eb2df13 4 Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
lixianyu 0:740c1eb2df13 5 Copyright 2011, Paul Stoffregen, paul@pjrc.com
lixianyu 0:740c1eb2df13 6
lixianyu 0:740c1eb2df13 7 This library is free software; you can redistribute it and/or
lixianyu 0:740c1eb2df13 8 modify it under the terms of the GNU Lesser General Public
lixianyu 0:740c1eb2df13 9 License as published by the Free Software Foundation; either
lixianyu 0:740c1eb2df13 10 version 2.1 of the License, or (at your option) any later version.
lixianyu 0:740c1eb2df13 11
lixianyu 0:740c1eb2df13 12 This library is distributed in the hope that it will be useful,
lixianyu 0:740c1eb2df13 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
lixianyu 0:740c1eb2df13 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lixianyu 0:740c1eb2df13 15 Lesser General Public License for more details.
lixianyu 0:740c1eb2df13 16
lixianyu 0:740c1eb2df13 17 You should have received a copy of the GNU Lesser General Public
lixianyu 0:740c1eb2df13 18 License along with this library; if not, write to the Free Software
lixianyu 0:740c1eb2df13 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lixianyu 0:740c1eb2df13 20 */
lixianyu 0:740c1eb2df13 21 #include <stdint.h>
lixianyu 0:740c1eb2df13 22 #include <stdlib.h>
lixianyu 0:740c1eb2df13 23 #include <stdio.h>
lixianyu 0:740c1eb2df13 24 #include <math.h>
lixianyu 0:740c1eb2df13 25 #include "WString.h"
lixianyu 0:740c1eb2df13 26
lixianyu 0:740c1eb2df13 27 /*整形转字符型*/
lixianyu 0:740c1eb2df13 28 char *itoa(int value, char *string, int radix)
lixianyu 0:740c1eb2df13 29 {
lixianyu 0:740c1eb2df13 30 char tmp[33];
lixianyu 0:740c1eb2df13 31 char *tp = tmp;
lixianyu 0:740c1eb2df13 32 int i;
lixianyu 0:740c1eb2df13 33 unsigned v;
lixianyu 0:740c1eb2df13 34 int sign;
lixianyu 0:740c1eb2df13 35 char *sp;
lixianyu 0:740c1eb2df13 36
lixianyu 0:740c1eb2df13 37 if (radix > 36 || radix <= 1) {
lixianyu 0:740c1eb2df13 38 //__set_errno(EDOM);
lixianyu 0:740c1eb2df13 39 return 0;
lixianyu 0:740c1eb2df13 40 }
lixianyu 0:740c1eb2df13 41
lixianyu 0:740c1eb2df13 42 sign = (radix == 10 && value < 0);
lixianyu 0:740c1eb2df13 43 if (sign)
lixianyu 0:740c1eb2df13 44 v = -value;
lixianyu 0:740c1eb2df13 45 else
lixianyu 0:740c1eb2df13 46 v = (unsigned)value;
lixianyu 0:740c1eb2df13 47 while (v || tp == tmp) {
lixianyu 0:740c1eb2df13 48 i = v % radix;
lixianyu 0:740c1eb2df13 49 v = v / radix;
lixianyu 0:740c1eb2df13 50 if (i < 10)
lixianyu 0:740c1eb2df13 51 *tp++ = i+'0';
lixianyu 0:740c1eb2df13 52 else
lixianyu 0:740c1eb2df13 53 *tp++ = i + 'a' - 10;
lixianyu 0:740c1eb2df13 54 }
lixianyu 0:740c1eb2df13 55
lixianyu 0:740c1eb2df13 56 if (string == 0)
lixianyu 0:740c1eb2df13 57 string = (char *)malloc((tp-tmp)+sign+1);
lixianyu 0:740c1eb2df13 58 sp = string;
lixianyu 0:740c1eb2df13 59
lixianyu 0:740c1eb2df13 60 if (sign)
lixianyu 0:740c1eb2df13 61 *sp++ = '-';
lixianyu 0:740c1eb2df13 62 while (tp > tmp)
lixianyu 0:740c1eb2df13 63 *sp++ = *--tp;
lixianyu 0:740c1eb2df13 64 *sp = 0;
lixianyu 0:740c1eb2df13 65 return string;
lixianyu 0:740c1eb2df13 66 }
lixianyu 0:740c1eb2df13 67
lixianyu 0:740c1eb2df13 68 /*长整形转字符型*/
lixianyu 0:740c1eb2df13 69 char *ltoa(long value, char *string, int radix)
lixianyu 0:740c1eb2df13 70 {
lixianyu 0:740c1eb2df13 71 char tmp[33];
lixianyu 0:740c1eb2df13 72 char *tp = tmp;
lixianyu 0:740c1eb2df13 73 long i;
lixianyu 0:740c1eb2df13 74 unsigned long v;
lixianyu 0:740c1eb2df13 75 int sign;
lixianyu 0:740c1eb2df13 76 char *sp;
lixianyu 0:740c1eb2df13 77
lixianyu 0:740c1eb2df13 78 if (radix > 36 || radix <= 1) {
lixianyu 0:740c1eb2df13 79 //__set_errno(EDOM);
lixianyu 0:740c1eb2df13 80 return 0;
lixianyu 0:740c1eb2df13 81 }
lixianyu 0:740c1eb2df13 82
lixianyu 0:740c1eb2df13 83 sign = (radix == 10 && value < 0);
lixianyu 0:740c1eb2df13 84 if (sign)
lixianyu 0:740c1eb2df13 85 v = -value;
lixianyu 0:740c1eb2df13 86 else
lixianyu 0:740c1eb2df13 87 v = (unsigned long)value;
lixianyu 0:740c1eb2df13 88 while (v || tp == tmp) {
lixianyu 0:740c1eb2df13 89 i = v % radix;
lixianyu 0:740c1eb2df13 90 v = v / radix;
lixianyu 0:740c1eb2df13 91 if (i < 10)
lixianyu 0:740c1eb2df13 92 *tp++ = i+'0';
lixianyu 0:740c1eb2df13 93 else
lixianyu 0:740c1eb2df13 94 *tp++ = i + 'a' - 10;
lixianyu 0:740c1eb2df13 95 }
lixianyu 0:740c1eb2df13 96
lixianyu 0:740c1eb2df13 97 if (string == 0)
lixianyu 0:740c1eb2df13 98 string = (char *)malloc((tp-tmp)+sign+1);
lixianyu 0:740c1eb2df13 99 sp = string;
lixianyu 0:740c1eb2df13 100
lixianyu 0:740c1eb2df13 101 if (sign)
lixianyu 0:740c1eb2df13 102 *sp++ = '-';
lixianyu 0:740c1eb2df13 103 while (tp > tmp)
lixianyu 0:740c1eb2df13 104 *sp++ = *--tp;
lixianyu 0:740c1eb2df13 105 *sp = 0;
lixianyu 0:740c1eb2df13 106 return string;
lixianyu 0:740c1eb2df13 107 }
lixianyu 0:740c1eb2df13 108
lixianyu 0:740c1eb2df13 109 /*无符号长整形转字符型*/
lixianyu 0:740c1eb2df13 110 char *ultoa(unsigned long value, char *string, int radix)
lixianyu 0:740c1eb2df13 111 {
lixianyu 0:740c1eb2df13 112 char tmp[33];
lixianyu 0:740c1eb2df13 113 char *tp = tmp;
lixianyu 0:740c1eb2df13 114 long i;
lixianyu 0:740c1eb2df13 115 unsigned long v = value;
lixianyu 0:740c1eb2df13 116 char *sp;
lixianyu 0:740c1eb2df13 117
lixianyu 0:740c1eb2df13 118 if (radix > 36 || radix <= 1) {
lixianyu 0:740c1eb2df13 119 //__set_errno(EDOM);
lixianyu 0:740c1eb2df13 120 return 0;
lixianyu 0:740c1eb2df13 121 }
lixianyu 0:740c1eb2df13 122
lixianyu 0:740c1eb2df13 123
lixianyu 0:740c1eb2df13 124 while (v || tp == tmp) {
lixianyu 0:740c1eb2df13 125 i = v % radix;
lixianyu 0:740c1eb2df13 126 v = v / radix;
lixianyu 0:740c1eb2df13 127 if (i < 10)
lixianyu 0:740c1eb2df13 128 *tp++ = i+'0';
lixianyu 0:740c1eb2df13 129 else
lixianyu 0:740c1eb2df13 130 *tp++ = i + 'a' - 10;
lixianyu 0:740c1eb2df13 131 }
lixianyu 0:740c1eb2df13 132 if (string == 0)
lixianyu 0:740c1eb2df13 133 string = (char *)malloc((tp-tmp)+1);
lixianyu 0:740c1eb2df13 134 sp = string;
lixianyu 0:740c1eb2df13 135
lixianyu 0:740c1eb2df13 136 while (tp > tmp)
lixianyu 0:740c1eb2df13 137 *sp++ = *--tp;
lixianyu 0:740c1eb2df13 138 *sp = 0;
lixianyu 0:740c1eb2df13 139 return string;
lixianyu 0:740c1eb2df13 140 }
lixianyu 0:740c1eb2df13 141
lixianyu 0:740c1eb2df13 142 /*字符串转整形*/
lixianyu 0:740c1eb2df13 143 //#include "iostream.h"
lixianyu 0:740c1eb2df13 144 int toi(const char * s)
lixianyu 0:740c1eb2df13 145 {
lixianyu 0:740c1eb2df13 146 int n = 0;
lixianyu 0:740c1eb2df13 147
lixianyu 0:740c1eb2df13 148 while(!(*s >= '0' && *s <= '9'))
lixianyu 0:740c1eb2df13 149 s++;
lixianyu 0:740c1eb2df13 150
lixianyu 0:740c1eb2df13 151 while(*s >= '0' && *s <= '9') {
lixianyu 0:740c1eb2df13 152 n *= 10;
lixianyu 0:740c1eb2df13 153 n += *s - '0';
lixianyu 0:740c1eb2df13 154 s++;
lixianyu 0:740c1eb2df13 155 }
lixianyu 0:740c1eb2df13 156 return n;
lixianyu 0:740c1eb2df13 157 }
lixianyu 0:740c1eb2df13 158 /*********************************************/
lixianyu 0:740c1eb2df13 159 /* Constructors */
lixianyu 0:740c1eb2df13 160 /*********************************************/
lixianyu 0:740c1eb2df13 161
lixianyu 0:740c1eb2df13 162 String::String(const char *cstr)
lixianyu 0:740c1eb2df13 163 {
lixianyu 0:740c1eb2df13 164 init();
lixianyu 0:740c1eb2df13 165 if (cstr) copy(cstr, strlen(cstr));
lixianyu 0:740c1eb2df13 166 }
lixianyu 0:740c1eb2df13 167
lixianyu 0:740c1eb2df13 168 String::String(const String &value)
lixianyu 0:740c1eb2df13 169 {
lixianyu 0:740c1eb2df13 170 init();
lixianyu 0:740c1eb2df13 171 *this = value;
lixianyu 0:740c1eb2df13 172 }
lixianyu 0:740c1eb2df13 173
lixianyu 0:740c1eb2df13 174 #if 0
lixianyu 0:740c1eb2df13 175 String::String(const __FlashStringHelper *pstr)
lixianyu 0:740c1eb2df13 176 {
lixianyu 0:740c1eb2df13 177 init();
lixianyu 0:740c1eb2df13 178 *this = pstr;
lixianyu 0:740c1eb2df13 179 }
lixianyu 0:740c1eb2df13 180 #endif
lixianyu 0:740c1eb2df13 181
lixianyu 0:740c1eb2df13 182 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:740c1eb2df13 183 String::String(String &&rval)
lixianyu 0:740c1eb2df13 184 {
lixianyu 0:740c1eb2df13 185 init();
lixianyu 0:740c1eb2df13 186 move(rval);
lixianyu 0:740c1eb2df13 187 }
lixianyu 0:740c1eb2df13 188 String::String(StringSumHelper &&rval)
lixianyu 0:740c1eb2df13 189 {
lixianyu 0:740c1eb2df13 190 init();
lixianyu 0:740c1eb2df13 191 move(rval);
lixianyu 0:740c1eb2df13 192 }
lixianyu 0:740c1eb2df13 193 #endif
lixianyu 0:740c1eb2df13 194
lixianyu 0:740c1eb2df13 195 String::String(char c)
lixianyu 0:740c1eb2df13 196 {
lixianyu 0:740c1eb2df13 197 init();
lixianyu 0:740c1eb2df13 198 char buf[2];
lixianyu 0:740c1eb2df13 199 buf[0] = c;
lixianyu 0:740c1eb2df13 200 buf[1] = 0;
lixianyu 0:740c1eb2df13 201 *this = buf;
lixianyu 0:740c1eb2df13 202 }
lixianyu 0:740c1eb2df13 203
lixianyu 0:740c1eb2df13 204 String::String(unsigned char value, unsigned char base)
lixianyu 0:740c1eb2df13 205 {
lixianyu 0:740c1eb2df13 206 init();
lixianyu 0:740c1eb2df13 207 char buf[1 + 8 * sizeof(unsigned char)];
lixianyu 0:740c1eb2df13 208 //utoa(value, buf, base);
lixianyu 0:740c1eb2df13 209 itoa(value, buf, base);
lixianyu 0:740c1eb2df13 210 *this = buf;
lixianyu 0:740c1eb2df13 211 }
lixianyu 0:740c1eb2df13 212
lixianyu 0:740c1eb2df13 213 String::String(int value, unsigned char base)
lixianyu 0:740c1eb2df13 214 {
lixianyu 0:740c1eb2df13 215 init();
lixianyu 0:740c1eb2df13 216 char buf[2 + 8 * sizeof(int)];
lixianyu 0:740c1eb2df13 217 itoa(value, buf, base);
lixianyu 0:740c1eb2df13 218 //sprintf(buf, "%x", value); //将100转为16进制表示的字符串。
lixianyu 0:740c1eb2df13 219 *this = buf;
lixianyu 0:740c1eb2df13 220 }
lixianyu 0:740c1eb2df13 221
lixianyu 0:740c1eb2df13 222 String::String(unsigned int value, unsigned char base)
lixianyu 0:740c1eb2df13 223 {
lixianyu 0:740c1eb2df13 224 init();
lixianyu 0:740c1eb2df13 225 char buf[1 + 8 * sizeof(unsigned int)];
lixianyu 0:740c1eb2df13 226 //utoa(value, buf, base);
lixianyu 0:740c1eb2df13 227 itoa(value, buf, base);
lixianyu 0:740c1eb2df13 228 *this = buf;
lixianyu 0:740c1eb2df13 229 }
lixianyu 0:740c1eb2df13 230
lixianyu 0:740c1eb2df13 231 String::String(long value, unsigned char base)
lixianyu 0:740c1eb2df13 232 {
lixianyu 0:740c1eb2df13 233 init();
lixianyu 0:740c1eb2df13 234 char buf[2 + 8 * sizeof(long)];
lixianyu 0:740c1eb2df13 235 ltoa(value, buf, base);
lixianyu 0:740c1eb2df13 236 *this = buf;
lixianyu 0:740c1eb2df13 237 }
lixianyu 0:740c1eb2df13 238
lixianyu 0:740c1eb2df13 239 String::String(unsigned long value, unsigned char base)
lixianyu 0:740c1eb2df13 240 {
lixianyu 0:740c1eb2df13 241 init();
lixianyu 0:740c1eb2df13 242 char buf[1 + 8 * sizeof(unsigned long)];
lixianyu 0:740c1eb2df13 243 //ultoa(value, buf, base);
lixianyu 0:740c1eb2df13 244 ultoa(value, buf, base);
lixianyu 0:740c1eb2df13 245 *this = buf;
lixianyu 0:740c1eb2df13 246 }
lixianyu 0:740c1eb2df13 247
lixianyu 0:740c1eb2df13 248 String::String(float value, unsigned char decimalPlaces)
lixianyu 0:740c1eb2df13 249 {
lixianyu 0:740c1eb2df13 250 init();
lixianyu 0:740c1eb2df13 251 char buf[33];
lixianyu 0:740c1eb2df13 252 #if 0 // TODO:
lixianyu 0:740c1eb2df13 253 *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
lixianyu 0:740c1eb2df13 254 #endif
lixianyu 0:740c1eb2df13 255 }
lixianyu 0:740c1eb2df13 256
lixianyu 0:740c1eb2df13 257 String::String(double value, unsigned char decimalPlaces)
lixianyu 0:740c1eb2df13 258 {
lixianyu 0:740c1eb2df13 259 init();
lixianyu 0:740c1eb2df13 260 char buf[33];
lixianyu 0:740c1eb2df13 261 #if 0 // TODO:
lixianyu 0:740c1eb2df13 262 *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
lixianyu 0:740c1eb2df13 263 #endif
lixianyu 0:740c1eb2df13 264 }
lixianyu 0:740c1eb2df13 265
lixianyu 0:740c1eb2df13 266 String::~String()
lixianyu 0:740c1eb2df13 267 {
lixianyu 0:740c1eb2df13 268 free(buffer);
lixianyu 0:740c1eb2df13 269 }
lixianyu 0:740c1eb2df13 270
lixianyu 0:740c1eb2df13 271 /*********************************************/
lixianyu 0:740c1eb2df13 272 /* Memory Management */
lixianyu 0:740c1eb2df13 273 /*********************************************/
lixianyu 0:740c1eb2df13 274
lixianyu 0:740c1eb2df13 275 inline void String::init(void)
lixianyu 0:740c1eb2df13 276 {
lixianyu 0:740c1eb2df13 277 buffer = NULL;
lixianyu 0:740c1eb2df13 278 capacity = 0;
lixianyu 0:740c1eb2df13 279 len = 0;
lixianyu 0:740c1eb2df13 280 }
lixianyu 0:740c1eb2df13 281
lixianyu 0:740c1eb2df13 282 void String::invalidate(void)
lixianyu 0:740c1eb2df13 283 {
lixianyu 0:740c1eb2df13 284 if (buffer) free(buffer);
lixianyu 0:740c1eb2df13 285 buffer = NULL;
lixianyu 0:740c1eb2df13 286 capacity = len = 0;
lixianyu 0:740c1eb2df13 287 }
lixianyu 0:740c1eb2df13 288
lixianyu 0:740c1eb2df13 289 unsigned char String::reserve(unsigned int size)
lixianyu 0:740c1eb2df13 290 {
lixianyu 0:740c1eb2df13 291 if (buffer && capacity >= size) return 1;
lixianyu 0:740c1eb2df13 292 if (changeBuffer(size)) {
lixianyu 0:740c1eb2df13 293 if (len == 0) buffer[0] = 0;
lixianyu 0:740c1eb2df13 294 return 1;
lixianyu 0:740c1eb2df13 295 }
lixianyu 0:740c1eb2df13 296 return 0;
lixianyu 0:740c1eb2df13 297 }
lixianyu 0:740c1eb2df13 298
lixianyu 0:740c1eb2df13 299 unsigned char String::changeBuffer(unsigned int maxStrLen)
lixianyu 0:740c1eb2df13 300 {
lixianyu 0:740c1eb2df13 301 char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
lixianyu 0:740c1eb2df13 302 if (newbuffer) {
lixianyu 0:740c1eb2df13 303 buffer = newbuffer;
lixianyu 0:740c1eb2df13 304 capacity = maxStrLen;
lixianyu 0:740c1eb2df13 305 return 1;
lixianyu 0:740c1eb2df13 306 }
lixianyu 0:740c1eb2df13 307 return 0;
lixianyu 0:740c1eb2df13 308 }
lixianyu 0:740c1eb2df13 309
lixianyu 0:740c1eb2df13 310 /*********************************************/
lixianyu 0:740c1eb2df13 311 /* Copy and Move */
lixianyu 0:740c1eb2df13 312 /*********************************************/
lixianyu 0:740c1eb2df13 313
lixianyu 0:740c1eb2df13 314 String & String::copy(const char *cstr, unsigned int length)
lixianyu 0:740c1eb2df13 315 {
lixianyu 0:740c1eb2df13 316 if (!reserve(length)) {
lixianyu 0:740c1eb2df13 317 invalidate();
lixianyu 0:740c1eb2df13 318 return *this;
lixianyu 0:740c1eb2df13 319 }
lixianyu 0:740c1eb2df13 320 len = length;
lixianyu 0:740c1eb2df13 321 strcpy(buffer, cstr);
lixianyu 0:740c1eb2df13 322 return *this;
lixianyu 0:740c1eb2df13 323 }
lixianyu 0:740c1eb2df13 324
lixianyu 0:740c1eb2df13 325 #if 0
lixianyu 0:740c1eb2df13 326 String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
lixianyu 0:740c1eb2df13 327 {
lixianyu 0:740c1eb2df13 328 if (!reserve(length)) {
lixianyu 0:740c1eb2df13 329 invalidate();
lixianyu 0:740c1eb2df13 330 return *this;
lixianyu 0:740c1eb2df13 331 }
lixianyu 0:740c1eb2df13 332 len = length;
lixianyu 0:740c1eb2df13 333 strcpy_P(buffer, (PGM_P)pstr);
lixianyu 0:740c1eb2df13 334 return *this;
lixianyu 0:740c1eb2df13 335 }
lixianyu 0:740c1eb2df13 336 #endif
lixianyu 0:740c1eb2df13 337
lixianyu 0:740c1eb2df13 338 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:740c1eb2df13 339 void String::move(String &rhs)
lixianyu 0:740c1eb2df13 340 {
lixianyu 0:740c1eb2df13 341 if (buffer) {
lixianyu 0:740c1eb2df13 342 if (capacity >= rhs.len) {
lixianyu 0:740c1eb2df13 343 strcpy(buffer, rhs.buffer);
lixianyu 0:740c1eb2df13 344 len = rhs.len;
lixianyu 0:740c1eb2df13 345 rhs.len = 0;
lixianyu 0:740c1eb2df13 346 return;
lixianyu 0:740c1eb2df13 347 } else {
lixianyu 0:740c1eb2df13 348 free(buffer);
lixianyu 0:740c1eb2df13 349 }
lixianyu 0:740c1eb2df13 350 }
lixianyu 0:740c1eb2df13 351 buffer = rhs.buffer;
lixianyu 0:740c1eb2df13 352 capacity = rhs.capacity;
lixianyu 0:740c1eb2df13 353 len = rhs.len;
lixianyu 0:740c1eb2df13 354 rhs.buffer = NULL;
lixianyu 0:740c1eb2df13 355 rhs.capacity = 0;
lixianyu 0:740c1eb2df13 356 rhs.len = 0;
lixianyu 0:740c1eb2df13 357 }
lixianyu 0:740c1eb2df13 358 #endif
lixianyu 0:740c1eb2df13 359
lixianyu 0:740c1eb2df13 360 String & String::operator = (const String &rhs)
lixianyu 0:740c1eb2df13 361 {
lixianyu 0:740c1eb2df13 362 if (this == &rhs) return *this;
lixianyu 0:740c1eb2df13 363
lixianyu 0:740c1eb2df13 364 if (rhs.buffer) copy(rhs.buffer, rhs.len);
lixianyu 0:740c1eb2df13 365 else invalidate();
lixianyu 0:740c1eb2df13 366
lixianyu 0:740c1eb2df13 367 return *this;
lixianyu 0:740c1eb2df13 368 }
lixianyu 0:740c1eb2df13 369
lixianyu 0:740c1eb2df13 370 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:740c1eb2df13 371 String & String::operator = (String &&rval)
lixianyu 0:740c1eb2df13 372 {
lixianyu 0:740c1eb2df13 373 if (this != &rval) move(rval);
lixianyu 0:740c1eb2df13 374 return *this;
lixianyu 0:740c1eb2df13 375 }
lixianyu 0:740c1eb2df13 376
lixianyu 0:740c1eb2df13 377 String & String::operator = (StringSumHelper &&rval)
lixianyu 0:740c1eb2df13 378 {
lixianyu 0:740c1eb2df13 379 if (this != &rval) move(rval);
lixianyu 0:740c1eb2df13 380 return *this;
lixianyu 0:740c1eb2df13 381 }
lixianyu 0:740c1eb2df13 382 #endif
lixianyu 0:740c1eb2df13 383
lixianyu 0:740c1eb2df13 384 String & String::operator = (const char *cstr)
lixianyu 0:740c1eb2df13 385 {
lixianyu 0:740c1eb2df13 386 if (cstr) copy(cstr, strlen(cstr));
lixianyu 0:740c1eb2df13 387 else invalidate();
lixianyu 0:740c1eb2df13 388
lixianyu 0:740c1eb2df13 389 return *this;
lixianyu 0:740c1eb2df13 390 }
lixianyu 0:740c1eb2df13 391
lixianyu 0:740c1eb2df13 392 #if 0
lixianyu 0:740c1eb2df13 393 String & String::operator = (const __FlashStringHelper *pstr)
lixianyu 0:740c1eb2df13 394 {
lixianyu 0:740c1eb2df13 395 if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
lixianyu 0:740c1eb2df13 396 else invalidate();
lixianyu 0:740c1eb2df13 397
lixianyu 0:740c1eb2df13 398 return *this;
lixianyu 0:740c1eb2df13 399 }
lixianyu 0:740c1eb2df13 400 #endif
lixianyu 0:740c1eb2df13 401
lixianyu 0:740c1eb2df13 402 /*********************************************/
lixianyu 0:740c1eb2df13 403 /* concat */
lixianyu 0:740c1eb2df13 404 /*********************************************/
lixianyu 0:740c1eb2df13 405
lixianyu 0:740c1eb2df13 406 unsigned char String::concat(const String &s)
lixianyu 0:740c1eb2df13 407 {
lixianyu 0:740c1eb2df13 408 return concat(s.buffer, s.len);
lixianyu 0:740c1eb2df13 409 }
lixianyu 0:740c1eb2df13 410
lixianyu 0:740c1eb2df13 411 unsigned char String::concat(const char *cstr, unsigned int length)
lixianyu 0:740c1eb2df13 412 {
lixianyu 0:740c1eb2df13 413 unsigned int newlen = len + length;
lixianyu 0:740c1eb2df13 414 if (!cstr) return 0;
lixianyu 0:740c1eb2df13 415 if (length == 0) return 1;
lixianyu 0:740c1eb2df13 416 if (!reserve(newlen)) return 0;
lixianyu 0:740c1eb2df13 417 strcpy(buffer + len, cstr);
lixianyu 0:740c1eb2df13 418 len = newlen;
lixianyu 0:740c1eb2df13 419 return 1;
lixianyu 0:740c1eb2df13 420 }
lixianyu 0:740c1eb2df13 421
lixianyu 0:740c1eb2df13 422 unsigned char String::concat(const char *cstr)
lixianyu 0:740c1eb2df13 423 {
lixianyu 0:740c1eb2df13 424 if (!cstr) return 0;
lixianyu 0:740c1eb2df13 425 return concat(cstr, strlen(cstr));
lixianyu 0:740c1eb2df13 426 }
lixianyu 0:740c1eb2df13 427
lixianyu 0:740c1eb2df13 428 unsigned char String::concat(char c)
lixianyu 0:740c1eb2df13 429 {
lixianyu 0:740c1eb2df13 430 char buf[2];
lixianyu 0:740c1eb2df13 431 buf[0] = c;
lixianyu 0:740c1eb2df13 432 buf[1] = 0;
lixianyu 0:740c1eb2df13 433 return concat(buf, 1);
lixianyu 0:740c1eb2df13 434 }
lixianyu 0:740c1eb2df13 435
lixianyu 0:740c1eb2df13 436 unsigned char String::concat(unsigned char num)
lixianyu 0:740c1eb2df13 437 {
lixianyu 0:740c1eb2df13 438 char buf[1 + 3 * sizeof(unsigned char)];
lixianyu 0:740c1eb2df13 439 itoa(num, buf, 10);
lixianyu 0:740c1eb2df13 440 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 441 }
lixianyu 0:740c1eb2df13 442
lixianyu 0:740c1eb2df13 443 unsigned char String::concat(int num)
lixianyu 0:740c1eb2df13 444 {
lixianyu 0:740c1eb2df13 445 char buf[2 + 3 * sizeof(int)];
lixianyu 0:740c1eb2df13 446 itoa(num, buf, 10);
lixianyu 0:740c1eb2df13 447 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 448 }
lixianyu 0:740c1eb2df13 449
lixianyu 0:740c1eb2df13 450 unsigned char String::concat(unsigned int num)
lixianyu 0:740c1eb2df13 451 {
lixianyu 0:740c1eb2df13 452 char buf[1 + 3 * sizeof(unsigned int)];
lixianyu 0:740c1eb2df13 453 //utoa(num, buf, 10);
lixianyu 0:740c1eb2df13 454 itoa(num, buf, 10);
lixianyu 0:740c1eb2df13 455 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 456 }
lixianyu 0:740c1eb2df13 457
lixianyu 0:740c1eb2df13 458 unsigned char String::concat(long num)
lixianyu 0:740c1eb2df13 459 {
lixianyu 0:740c1eb2df13 460 char buf[2 + 3 * sizeof(long)];
lixianyu 0:740c1eb2df13 461 ltoa(num, buf, 10);
lixianyu 0:740c1eb2df13 462 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 463 }
lixianyu 0:740c1eb2df13 464
lixianyu 0:740c1eb2df13 465 unsigned char String::concat(unsigned long num)
lixianyu 0:740c1eb2df13 466 {
lixianyu 0:740c1eb2df13 467 char buf[1 + 3 * sizeof(unsigned long)];
lixianyu 0:740c1eb2df13 468 ultoa(num, buf, 10);
lixianyu 0:740c1eb2df13 469 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 470 }
lixianyu 0:740c1eb2df13 471
lixianyu 0:740c1eb2df13 472 unsigned char String::concat(float num)
lixianyu 0:740c1eb2df13 473 {
lixianyu 0:740c1eb2df13 474 char buf[20];
lixianyu 0:740c1eb2df13 475 //char* string = dtostrf(num, 4, 2, buf);
lixianyu 0:740c1eb2df13 476 sprintf(buf, "%6.2f", num); //将100转为16进制表示的字符串。
lixianyu 0:740c1eb2df13 477 //return concat(string, strlen(string));
lixianyu 0:740c1eb2df13 478 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 479 }
lixianyu 0:740c1eb2df13 480
lixianyu 0:740c1eb2df13 481 unsigned char String::concat(double num)
lixianyu 0:740c1eb2df13 482 {
lixianyu 0:740c1eb2df13 483 char buf[20];
lixianyu 0:740c1eb2df13 484 //char* string = dtostrf(num, 4, 2, buf);
lixianyu 0:740c1eb2df13 485 sprintf(buf, "%6.2f", num); //将100转为16进制表示的字符串。
lixianyu 0:740c1eb2df13 486 //return concat(string, strlen(string));
lixianyu 0:740c1eb2df13 487 return concat(buf, strlen(buf));
lixianyu 0:740c1eb2df13 488 }
lixianyu 0:740c1eb2df13 489 #if 0
lixianyu 0:740c1eb2df13 490 unsigned char String::concat(const __FlashStringHelper * str)
lixianyu 0:740c1eb2df13 491 {
lixianyu 0:740c1eb2df13 492 if (!str) return 0;
lixianyu 0:740c1eb2df13 493 int length = strlen_P((const char *) str);
lixianyu 0:740c1eb2df13 494 if (length == 0) return 1;
lixianyu 0:740c1eb2df13 495 unsigned int newlen = len + length;
lixianyu 0:740c1eb2df13 496 if (!reserve(newlen)) return 0;
lixianyu 0:740c1eb2df13 497 strcpy_P(buffer + len, (const char *) str);
lixianyu 0:740c1eb2df13 498 len = newlen;
lixianyu 0:740c1eb2df13 499 return 1;
lixianyu 0:740c1eb2df13 500 }
lixianyu 0:740c1eb2df13 501 #endif
lixianyu 0:740c1eb2df13 502 /*********************************************/
lixianyu 0:740c1eb2df13 503 /* Concatenate */
lixianyu 0:740c1eb2df13 504 /*********************************************/
lixianyu 0:740c1eb2df13 505
lixianyu 0:740c1eb2df13 506 StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
lixianyu 0:740c1eb2df13 507 {
lixianyu 0:740c1eb2df13 508 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 509 if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
lixianyu 0:740c1eb2df13 510 return a;
lixianyu 0:740c1eb2df13 511 }
lixianyu 0:740c1eb2df13 512
lixianyu 0:740c1eb2df13 513 StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
lixianyu 0:740c1eb2df13 514 {
lixianyu 0:740c1eb2df13 515 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 516 if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
lixianyu 0:740c1eb2df13 517 return a;
lixianyu 0:740c1eb2df13 518 }
lixianyu 0:740c1eb2df13 519
lixianyu 0:740c1eb2df13 520 StringSumHelper & operator + (const StringSumHelper &lhs, char c)
lixianyu 0:740c1eb2df13 521 {
lixianyu 0:740c1eb2df13 522 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 523 if (!a.concat(c)) a.invalidate();
lixianyu 0:740c1eb2df13 524 return a;
lixianyu 0:740c1eb2df13 525 }
lixianyu 0:740c1eb2df13 526
lixianyu 0:740c1eb2df13 527 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
lixianyu 0:740c1eb2df13 528 {
lixianyu 0:740c1eb2df13 529 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 530 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 531 return a;
lixianyu 0:740c1eb2df13 532 }
lixianyu 0:740c1eb2df13 533
lixianyu 0:740c1eb2df13 534 StringSumHelper & operator + (const StringSumHelper &lhs, int num)
lixianyu 0:740c1eb2df13 535 {
lixianyu 0:740c1eb2df13 536 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 537 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 538 return a;
lixianyu 0:740c1eb2df13 539 }
lixianyu 0:740c1eb2df13 540
lixianyu 0:740c1eb2df13 541 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
lixianyu 0:740c1eb2df13 542 {
lixianyu 0:740c1eb2df13 543 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 544 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 545 return a;
lixianyu 0:740c1eb2df13 546 }
lixianyu 0:740c1eb2df13 547
lixianyu 0:740c1eb2df13 548 StringSumHelper & operator + (const StringSumHelper &lhs, long num)
lixianyu 0:740c1eb2df13 549 {
lixianyu 0:740c1eb2df13 550 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 551 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 552 return a;
lixianyu 0:740c1eb2df13 553 }
lixianyu 0:740c1eb2df13 554
lixianyu 0:740c1eb2df13 555 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
lixianyu 0:740c1eb2df13 556 {
lixianyu 0:740c1eb2df13 557 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 558 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 559 return a;
lixianyu 0:740c1eb2df13 560 }
lixianyu 0:740c1eb2df13 561
lixianyu 0:740c1eb2df13 562 StringSumHelper & operator + (const StringSumHelper &lhs, float num)
lixianyu 0:740c1eb2df13 563 {
lixianyu 0:740c1eb2df13 564 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 565 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 566 return a;
lixianyu 0:740c1eb2df13 567 }
lixianyu 0:740c1eb2df13 568
lixianyu 0:740c1eb2df13 569 StringSumHelper & operator + (const StringSumHelper &lhs, double num)
lixianyu 0:740c1eb2df13 570 {
lixianyu 0:740c1eb2df13 571 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 572 if (!a.concat(num)) a.invalidate();
lixianyu 0:740c1eb2df13 573 return a;
lixianyu 0:740c1eb2df13 574 }
lixianyu 0:740c1eb2df13 575
lixianyu 0:740c1eb2df13 576 #if 0
lixianyu 0:740c1eb2df13 577 StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
lixianyu 0:740c1eb2df13 578 {
lixianyu 0:740c1eb2df13 579 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
lixianyu 0:740c1eb2df13 580 if (!a.concat(rhs)) a.invalidate();
lixianyu 0:740c1eb2df13 581 return a;
lixianyu 0:740c1eb2df13 582 }
lixianyu 0:740c1eb2df13 583 #endif
lixianyu 0:740c1eb2df13 584 /*********************************************/
lixianyu 0:740c1eb2df13 585 /* Comparison */
lixianyu 0:740c1eb2df13 586 /*********************************************/
lixianyu 0:740c1eb2df13 587
lixianyu 0:740c1eb2df13 588 int String::compareTo(const String &s) const
lixianyu 0:740c1eb2df13 589 {
lixianyu 0:740c1eb2df13 590 if (!buffer || !s.buffer) {
lixianyu 0:740c1eb2df13 591 if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
lixianyu 0:740c1eb2df13 592 if (buffer && len > 0) return *(unsigned char *)buffer;
lixianyu 0:740c1eb2df13 593 return 0;
lixianyu 0:740c1eb2df13 594 }
lixianyu 0:740c1eb2df13 595 return strcmp(buffer, s.buffer);
lixianyu 0:740c1eb2df13 596 }
lixianyu 0:740c1eb2df13 597
lixianyu 0:740c1eb2df13 598 unsigned char String::equals(const String &s2) const
lixianyu 0:740c1eb2df13 599 {
lixianyu 0:740c1eb2df13 600 return (len == s2.len && compareTo(s2) == 0);
lixianyu 0:740c1eb2df13 601 }
lixianyu 0:740c1eb2df13 602
lixianyu 0:740c1eb2df13 603 unsigned char String::equals(const char *cstr) const
lixianyu 0:740c1eb2df13 604 {
lixianyu 0:740c1eb2df13 605 if (len == 0) return (cstr == NULL || *cstr == 0);
lixianyu 0:740c1eb2df13 606 if (cstr == NULL) return buffer[0] == 0;
lixianyu 0:740c1eb2df13 607 return strcmp(buffer, cstr) == 0;
lixianyu 0:740c1eb2df13 608 }
lixianyu 0:740c1eb2df13 609
lixianyu 0:740c1eb2df13 610 unsigned char String::operator<(const String &rhs) const
lixianyu 0:740c1eb2df13 611 {
lixianyu 0:740c1eb2df13 612 return compareTo(rhs) < 0;
lixianyu 0:740c1eb2df13 613 }
lixianyu 0:740c1eb2df13 614
lixianyu 0:740c1eb2df13 615 unsigned char String::operator>(const String &rhs) const
lixianyu 0:740c1eb2df13 616 {
lixianyu 0:740c1eb2df13 617 return compareTo(rhs) > 0;
lixianyu 0:740c1eb2df13 618 }
lixianyu 0:740c1eb2df13 619
lixianyu 0:740c1eb2df13 620 unsigned char String::operator<=(const String &rhs) const
lixianyu 0:740c1eb2df13 621 {
lixianyu 0:740c1eb2df13 622 return compareTo(rhs) <= 0;
lixianyu 0:740c1eb2df13 623 }
lixianyu 0:740c1eb2df13 624
lixianyu 0:740c1eb2df13 625 unsigned char String::operator>=(const String &rhs) const
lixianyu 0:740c1eb2df13 626 {
lixianyu 0:740c1eb2df13 627 return compareTo(rhs) >= 0;
lixianyu 0:740c1eb2df13 628 }
lixianyu 0:740c1eb2df13 629
lixianyu 0:740c1eb2df13 630 unsigned char String::equalsIgnoreCase( const String &s2 ) const
lixianyu 0:740c1eb2df13 631 {
lixianyu 0:740c1eb2df13 632 if (this == &s2) return 1;
lixianyu 0:740c1eb2df13 633 if (len != s2.len) return 0;
lixianyu 0:740c1eb2df13 634 if (len == 0) return 1;
lixianyu 0:740c1eb2df13 635 const char *p1 = buffer;
lixianyu 0:740c1eb2df13 636 const char *p2 = s2.buffer;
lixianyu 0:740c1eb2df13 637 while (*p1) {
lixianyu 0:740c1eb2df13 638 if (tolower(*p1++) != tolower(*p2++)) return 0;
lixianyu 0:740c1eb2df13 639 }
lixianyu 0:740c1eb2df13 640 return 1;
lixianyu 0:740c1eb2df13 641 }
lixianyu 0:740c1eb2df13 642
lixianyu 0:740c1eb2df13 643 unsigned char String::startsWith( const String &s2 ) const
lixianyu 0:740c1eb2df13 644 {
lixianyu 0:740c1eb2df13 645 if (len < s2.len) return 0;
lixianyu 0:740c1eb2df13 646 return startsWith(s2, 0);
lixianyu 0:740c1eb2df13 647 }
lixianyu 0:740c1eb2df13 648
lixianyu 0:740c1eb2df13 649 unsigned char String::startsWith( const String &s2, unsigned int offset ) const
lixianyu 0:740c1eb2df13 650 {
lixianyu 0:740c1eb2df13 651 if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
lixianyu 0:740c1eb2df13 652 return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
lixianyu 0:740c1eb2df13 653 }
lixianyu 0:740c1eb2df13 654
lixianyu 0:740c1eb2df13 655 unsigned char String::endsWith( const String &s2 ) const
lixianyu 0:740c1eb2df13 656 {
lixianyu 0:740c1eb2df13 657 if ( len < s2.len || !buffer || !s2.buffer) return 0;
lixianyu 0:740c1eb2df13 658 return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
lixianyu 0:740c1eb2df13 659 }
lixianyu 0:740c1eb2df13 660
lixianyu 0:740c1eb2df13 661 /*********************************************/
lixianyu 0:740c1eb2df13 662 /* Character Access */
lixianyu 0:740c1eb2df13 663 /*********************************************/
lixianyu 0:740c1eb2df13 664
lixianyu 0:740c1eb2df13 665 char String::charAt(unsigned int loc) const
lixianyu 0:740c1eb2df13 666 {
lixianyu 0:740c1eb2df13 667 return operator[](loc);
lixianyu 0:740c1eb2df13 668 }
lixianyu 0:740c1eb2df13 669
lixianyu 0:740c1eb2df13 670 void String::setCharAt(unsigned int loc, char c)
lixianyu 0:740c1eb2df13 671 {
lixianyu 0:740c1eb2df13 672 if (loc < len) buffer[loc] = c;
lixianyu 0:740c1eb2df13 673 }
lixianyu 0:740c1eb2df13 674
lixianyu 0:740c1eb2df13 675 char & String::operator[](unsigned int index)
lixianyu 0:740c1eb2df13 676 {
lixianyu 0:740c1eb2df13 677 static char dummy_writable_char;
lixianyu 0:740c1eb2df13 678 if (index >= len || !buffer) {
lixianyu 0:740c1eb2df13 679 dummy_writable_char = 0;
lixianyu 0:740c1eb2df13 680 return dummy_writable_char;
lixianyu 0:740c1eb2df13 681 }
lixianyu 0:740c1eb2df13 682 return buffer[index];
lixianyu 0:740c1eb2df13 683 }
lixianyu 0:740c1eb2df13 684
lixianyu 0:740c1eb2df13 685 char String::operator[]( unsigned int index ) const
lixianyu 0:740c1eb2df13 686 {
lixianyu 0:740c1eb2df13 687 if (index >= len || !buffer) return 0;
lixianyu 0:740c1eb2df13 688 return buffer[index];
lixianyu 0:740c1eb2df13 689 }
lixianyu 0:740c1eb2df13 690
lixianyu 0:740c1eb2df13 691 void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
lixianyu 0:740c1eb2df13 692 {
lixianyu 0:740c1eb2df13 693 if (!bufsize || !buf) return;
lixianyu 0:740c1eb2df13 694 if (index >= len) {
lixianyu 0:740c1eb2df13 695 buf[0] = 0;
lixianyu 0:740c1eb2df13 696 return;
lixianyu 0:740c1eb2df13 697 }
lixianyu 0:740c1eb2df13 698 unsigned int n = bufsize - 1;
lixianyu 0:740c1eb2df13 699 if (n > len - index) n = len - index;
lixianyu 0:740c1eb2df13 700 strncpy((char *)buf, buffer + index, n);
lixianyu 0:740c1eb2df13 701 buf[n] = 0;
lixianyu 0:740c1eb2df13 702 }
lixianyu 0:740c1eb2df13 703
lixianyu 0:740c1eb2df13 704 /*********************************************/
lixianyu 0:740c1eb2df13 705 /* Search */
lixianyu 0:740c1eb2df13 706 /*********************************************/
lixianyu 0:740c1eb2df13 707
lixianyu 0:740c1eb2df13 708 int String::indexOf(char c) const
lixianyu 0:740c1eb2df13 709 {
lixianyu 0:740c1eb2df13 710 return indexOf(c, 0);
lixianyu 0:740c1eb2df13 711 }
lixianyu 0:740c1eb2df13 712
lixianyu 0:740c1eb2df13 713 int String::indexOf( char ch, unsigned int fromIndex ) const
lixianyu 0:740c1eb2df13 714 {
lixianyu 0:740c1eb2df13 715 if (fromIndex >= len) return -1;
lixianyu 0:740c1eb2df13 716 const char* temp = strchr(buffer + fromIndex, ch);
lixianyu 0:740c1eb2df13 717 if (temp == NULL) return -1;
lixianyu 0:740c1eb2df13 718 return temp - buffer;
lixianyu 0:740c1eb2df13 719 }
lixianyu 0:740c1eb2df13 720
lixianyu 0:740c1eb2df13 721 int String::indexOf(const String &s2) const
lixianyu 0:740c1eb2df13 722 {
lixianyu 0:740c1eb2df13 723 return indexOf(s2, 0);
lixianyu 0:740c1eb2df13 724 }
lixianyu 0:740c1eb2df13 725
lixianyu 0:740c1eb2df13 726 int String::indexOf(const String &s2, unsigned int fromIndex) const
lixianyu 0:740c1eb2df13 727 {
lixianyu 0:740c1eb2df13 728 if (fromIndex >= len) return -1;
lixianyu 0:740c1eb2df13 729 const char *found = strstr(buffer + fromIndex, s2.buffer);
lixianyu 0:740c1eb2df13 730 if (found == NULL) return -1;
lixianyu 0:740c1eb2df13 731 return found - buffer;
lixianyu 0:740c1eb2df13 732 }
lixianyu 0:740c1eb2df13 733
lixianyu 0:740c1eb2df13 734 int String::lastIndexOf( char theChar ) const
lixianyu 0:740c1eb2df13 735 {
lixianyu 0:740c1eb2df13 736 return lastIndexOf(theChar, len - 1);
lixianyu 0:740c1eb2df13 737 }
lixianyu 0:740c1eb2df13 738
lixianyu 0:740c1eb2df13 739 int String::lastIndexOf(char ch, unsigned int fromIndex) const
lixianyu 0:740c1eb2df13 740 {
lixianyu 0:740c1eb2df13 741 if (fromIndex >= len) return -1;
lixianyu 0:740c1eb2df13 742 char tempchar = buffer[fromIndex + 1];
lixianyu 0:740c1eb2df13 743 buffer[fromIndex + 1] = '\0';
lixianyu 0:740c1eb2df13 744 char* temp = strrchr( buffer, ch );
lixianyu 0:740c1eb2df13 745 buffer[fromIndex + 1] = tempchar;
lixianyu 0:740c1eb2df13 746 if (temp == NULL) return -1;
lixianyu 0:740c1eb2df13 747 return temp - buffer;
lixianyu 0:740c1eb2df13 748 }
lixianyu 0:740c1eb2df13 749
lixianyu 0:740c1eb2df13 750 int String::lastIndexOf(const String &s2) const
lixianyu 0:740c1eb2df13 751 {
lixianyu 0:740c1eb2df13 752 return lastIndexOf(s2, len - s2.len);
lixianyu 0:740c1eb2df13 753 }
lixianyu 0:740c1eb2df13 754
lixianyu 0:740c1eb2df13 755 int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
lixianyu 0:740c1eb2df13 756 {
lixianyu 0:740c1eb2df13 757 if (s2.len == 0 || len == 0 || s2.len > len) return -1;
lixianyu 0:740c1eb2df13 758 if (fromIndex >= len) fromIndex = len - 1;
lixianyu 0:740c1eb2df13 759 int found = -1;
lixianyu 0:740c1eb2df13 760 for (char *p = buffer; p <= buffer + fromIndex; p++) {
lixianyu 0:740c1eb2df13 761 p = strstr(p, s2.buffer);
lixianyu 0:740c1eb2df13 762 if (!p) break;
lixianyu 0:740c1eb2df13 763 if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
lixianyu 0:740c1eb2df13 764 }
lixianyu 0:740c1eb2df13 765 return found;
lixianyu 0:740c1eb2df13 766 }
lixianyu 0:740c1eb2df13 767
lixianyu 0:740c1eb2df13 768 String String::substring(unsigned int left, unsigned int right) const
lixianyu 0:740c1eb2df13 769 {
lixianyu 0:740c1eb2df13 770 if (left > right) {
lixianyu 0:740c1eb2df13 771 unsigned int temp = right;
lixianyu 0:740c1eb2df13 772 right = left;
lixianyu 0:740c1eb2df13 773 left = temp;
lixianyu 0:740c1eb2df13 774 }
lixianyu 0:740c1eb2df13 775 String out;
lixianyu 0:740c1eb2df13 776 if (left >= len) return out;
lixianyu 0:740c1eb2df13 777 if (right > len) right = len;
lixianyu 0:740c1eb2df13 778 char temp = buffer[right]; // save the replaced character
lixianyu 0:740c1eb2df13 779 buffer[right] = '\0';
lixianyu 0:740c1eb2df13 780 out = buffer + left; // pointer arithmetic
lixianyu 0:740c1eb2df13 781 buffer[right] = temp; //restore character
lixianyu 0:740c1eb2df13 782 return out;
lixianyu 0:740c1eb2df13 783 }
lixianyu 0:740c1eb2df13 784
lixianyu 0:740c1eb2df13 785 /*********************************************/
lixianyu 0:740c1eb2df13 786 /* Modification */
lixianyu 0:740c1eb2df13 787 /*********************************************/
lixianyu 0:740c1eb2df13 788
lixianyu 0:740c1eb2df13 789 void String::replace(char find, char replace)
lixianyu 0:740c1eb2df13 790 {
lixianyu 0:740c1eb2df13 791 if (!buffer) return;
lixianyu 0:740c1eb2df13 792 for (char *p = buffer; *p; p++) {
lixianyu 0:740c1eb2df13 793 if (*p == find) *p = replace;
lixianyu 0:740c1eb2df13 794 }
lixianyu 0:740c1eb2df13 795 }
lixianyu 0:740c1eb2df13 796
lixianyu 0:740c1eb2df13 797 void String::replace(const String& find, const String& replace)
lixianyu 0:740c1eb2df13 798 {
lixianyu 0:740c1eb2df13 799 if (len == 0 || find.len == 0) return;
lixianyu 0:740c1eb2df13 800 int diff = replace.len - find.len;
lixianyu 0:740c1eb2df13 801 char *readFrom = buffer;
lixianyu 0:740c1eb2df13 802 char *foundAt;
lixianyu 0:740c1eb2df13 803 if (diff == 0) {
lixianyu 0:740c1eb2df13 804 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
lixianyu 0:740c1eb2df13 805 memcpy(foundAt, replace.buffer, replace.len);
lixianyu 0:740c1eb2df13 806 readFrom = foundAt + replace.len;
lixianyu 0:740c1eb2df13 807 }
lixianyu 0:740c1eb2df13 808 } else if (diff < 0) {
lixianyu 0:740c1eb2df13 809 char *writeTo = buffer;
lixianyu 0:740c1eb2df13 810 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
lixianyu 0:740c1eb2df13 811 unsigned int n = foundAt - readFrom;
lixianyu 0:740c1eb2df13 812 memcpy(writeTo, readFrom, n);
lixianyu 0:740c1eb2df13 813 writeTo += n;
lixianyu 0:740c1eb2df13 814 memcpy(writeTo, replace.buffer, replace.len);
lixianyu 0:740c1eb2df13 815 writeTo += replace.len;
lixianyu 0:740c1eb2df13 816 readFrom = foundAt + find.len;
lixianyu 0:740c1eb2df13 817 len += diff;
lixianyu 0:740c1eb2df13 818 }
lixianyu 0:740c1eb2df13 819 strcpy(writeTo, readFrom);
lixianyu 0:740c1eb2df13 820 } else {
lixianyu 0:740c1eb2df13 821 unsigned int size = len; // compute size needed for result
lixianyu 0:740c1eb2df13 822 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
lixianyu 0:740c1eb2df13 823 readFrom = foundAt + find.len;
lixianyu 0:740c1eb2df13 824 size += diff;
lixianyu 0:740c1eb2df13 825 }
lixianyu 0:740c1eb2df13 826 if (size == len) return;
lixianyu 0:740c1eb2df13 827 if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
lixianyu 0:740c1eb2df13 828 int index = len - 1;
lixianyu 0:740c1eb2df13 829 while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
lixianyu 0:740c1eb2df13 830 readFrom = buffer + index + find.len;
lixianyu 0:740c1eb2df13 831 memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
lixianyu 0:740c1eb2df13 832 len += diff;
lixianyu 0:740c1eb2df13 833 buffer[len] = 0;
lixianyu 0:740c1eb2df13 834 memcpy(buffer + index, replace.buffer, replace.len);
lixianyu 0:740c1eb2df13 835 index--;
lixianyu 0:740c1eb2df13 836 }
lixianyu 0:740c1eb2df13 837 }
lixianyu 0:740c1eb2df13 838 }
lixianyu 0:740c1eb2df13 839
lixianyu 0:740c1eb2df13 840 void String::remove(unsigned int index)
lixianyu 0:740c1eb2df13 841 {
lixianyu 0:740c1eb2df13 842 // Pass the biggest integer as the count. The remove method
lixianyu 0:740c1eb2df13 843 // below will take care of truncating it at the end of the
lixianyu 0:740c1eb2df13 844 // string.
lixianyu 0:740c1eb2df13 845 remove(index, (unsigned int)-1);
lixianyu 0:740c1eb2df13 846 }
lixianyu 0:740c1eb2df13 847
lixianyu 0:740c1eb2df13 848 void String::remove(unsigned int index, unsigned int count)
lixianyu 0:740c1eb2df13 849 {
lixianyu 0:740c1eb2df13 850 if (index >= len) {
lixianyu 0:740c1eb2df13 851 return;
lixianyu 0:740c1eb2df13 852 }
lixianyu 0:740c1eb2df13 853 if (count <= 0) {
lixianyu 0:740c1eb2df13 854 return;
lixianyu 0:740c1eb2df13 855 }
lixianyu 0:740c1eb2df13 856 if (count > len - index) {
lixianyu 0:740c1eb2df13 857 count = len - index;
lixianyu 0:740c1eb2df13 858 }
lixianyu 0:740c1eb2df13 859 char *writeTo = buffer + index;
lixianyu 0:740c1eb2df13 860 len = len - count;
lixianyu 0:740c1eb2df13 861 strncpy(writeTo, buffer + index + count,len - index);
lixianyu 0:740c1eb2df13 862 buffer[len] = 0;
lixianyu 0:740c1eb2df13 863 }
lixianyu 0:740c1eb2df13 864
lixianyu 0:740c1eb2df13 865 void String::toLowerCase(void)
lixianyu 0:740c1eb2df13 866 {
lixianyu 0:740c1eb2df13 867 if (!buffer) return;
lixianyu 0:740c1eb2df13 868 for (char *p = buffer; *p; p++) {
lixianyu 0:740c1eb2df13 869 *p = tolower(*p);
lixianyu 0:740c1eb2df13 870 }
lixianyu 0:740c1eb2df13 871 }
lixianyu 0:740c1eb2df13 872
lixianyu 0:740c1eb2df13 873 void String::toUpperCase(void)
lixianyu 0:740c1eb2df13 874 {
lixianyu 0:740c1eb2df13 875 if (!buffer) return;
lixianyu 0:740c1eb2df13 876 for (char *p = buffer; *p; p++) {
lixianyu 0:740c1eb2df13 877 *p = toupper(*p);
lixianyu 0:740c1eb2df13 878 }
lixianyu 0:740c1eb2df13 879 }
lixianyu 0:740c1eb2df13 880
lixianyu 0:740c1eb2df13 881 void String::trim(void)
lixianyu 0:740c1eb2df13 882 {
lixianyu 0:740c1eb2df13 883 if (!buffer || len == 0) return;
lixianyu 0:740c1eb2df13 884 char *begin = buffer;
lixianyu 0:740c1eb2df13 885 while (isspace(*begin)) begin++;
lixianyu 0:740c1eb2df13 886 char *end = buffer + len - 1;
lixianyu 0:740c1eb2df13 887 while (isspace(*end) && end >= begin) end--;
lixianyu 0:740c1eb2df13 888 len = end + 1 - begin;
lixianyu 0:740c1eb2df13 889 if (begin > buffer) memcpy(buffer, begin, len);
lixianyu 0:740c1eb2df13 890 buffer[len] = 0;
lixianyu 0:740c1eb2df13 891 }
lixianyu 0:740c1eb2df13 892
lixianyu 0:740c1eb2df13 893 /*********************************************/
lixianyu 0:740c1eb2df13 894 /* Parsing / Conversion */
lixianyu 0:740c1eb2df13 895 /*********************************************/
lixianyu 0:740c1eb2df13 896
lixianyu 0:740c1eb2df13 897 long String::toInt(void) const
lixianyu 0:740c1eb2df13 898 {
lixianyu 0:740c1eb2df13 899 if (buffer) return atol(buffer);
lixianyu 0:740c1eb2df13 900 return 0;
lixianyu 0:740c1eb2df13 901 }
lixianyu 0:740c1eb2df13 902
lixianyu 0:740c1eb2df13 903 float String::toFloat(void) const
lixianyu 0:740c1eb2df13 904 {
lixianyu 0:740c1eb2df13 905 if (buffer) return float(atof(buffer));
lixianyu 0:740c1eb2df13 906 return 0;
lixianyu 0:740c1eb2df13 907 }
lixianyu 0:740c1eb2df13 908