work.

Dependencies:   Blynk mbed

Committer:
lixianyu
Date:
Thu Jun 16 08:12:33 2016 +0000
Revision:
4:e5018e5ba340
Parent:
0:d8f4c441e032
ok

Who changed what in which revision?

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