leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16 #include "mbed-client/m2mstring.h"
leothedragon 0:8f0bb79ddd48 17 #include <string.h> // strlen
leothedragon 0:8f0bb79ddd48 18 #include <stdlib.h> // malloc, realloc
leothedragon 0:8f0bb79ddd48 19 #include <assert.h>
leothedragon 0:8f0bb79ddd48 20
leothedragon 0:8f0bb79ddd48 21 namespace m2m {
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 // can't use std::min as it is not universally available
leothedragon 0:8f0bb79ddd48 25 #ifndef MIN
leothedragon 0:8f0bb79ddd48 26 #define MIN(A,B) ((A) < (B) ? (A) : (B))
leothedragon 0:8f0bb79ddd48 27 #endif
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 const String::size_type String::npos = static_cast<size_t>(-1);
leothedragon 0:8f0bb79ddd48 30
leothedragon 0:8f0bb79ddd48 31 char* String::strdup(const char* s)
leothedragon 0:8f0bb79ddd48 32 {
leothedragon 0:8f0bb79ddd48 33 const size_t len = strlen(s)+1;
leothedragon 0:8f0bb79ddd48 34 char *p2 = static_cast<char*>(malloc(len));
leothedragon 0:8f0bb79ddd48 35 if (p2) {
leothedragon 0:8f0bb79ddd48 36 memcpy(p2, s, len);
leothedragon 0:8f0bb79ddd48 37 allocated_ = len;
leothedragon 0:8f0bb79ddd48 38 size_ = len-1;
leothedragon 0:8f0bb79ddd48 39 }
leothedragon 0:8f0bb79ddd48 40 return p2;
leothedragon 0:8f0bb79ddd48 41 }
leothedragon 0:8f0bb79ddd48 42
leothedragon 0:8f0bb79ddd48 43 String::String()
leothedragon 0:8f0bb79ddd48 44 : p( strdup("") )
leothedragon 0:8f0bb79ddd48 45 {
leothedragon 0:8f0bb79ddd48 46 }
leothedragon 0:8f0bb79ddd48 47
leothedragon 0:8f0bb79ddd48 48 String::~String()
leothedragon 0:8f0bb79ddd48 49 {
leothedragon 0:8f0bb79ddd48 50 free(p);
leothedragon 0:8f0bb79ddd48 51 p = 0;
leothedragon 0:8f0bb79ddd48 52 }
leothedragon 0:8f0bb79ddd48 53
leothedragon 0:8f0bb79ddd48 54 String::String(const String& s)
leothedragon 0:8f0bb79ddd48 55 : p(0)
leothedragon 0:8f0bb79ddd48 56 {
leothedragon 0:8f0bb79ddd48 57 p = static_cast<char*>(malloc(s.size_ + 1));
leothedragon 0:8f0bb79ddd48 58
leothedragon 0:8f0bb79ddd48 59 allocated_ = s.size_ + 1;
leothedragon 0:8f0bb79ddd48 60 size_ = s.size_;
leothedragon 0:8f0bb79ddd48 61 memcpy(p, s.p, size_ + 1);
leothedragon 0:8f0bb79ddd48 62 }
leothedragon 0:8f0bb79ddd48 63
leothedragon 0:8f0bb79ddd48 64 String::String(const char* s)
leothedragon 0:8f0bb79ddd48 65 : p(strdup(s))
leothedragon 0:8f0bb79ddd48 66 {
leothedragon 0:8f0bb79ddd48 67 }
leothedragon 0:8f0bb79ddd48 68
leothedragon 0:8f0bb79ddd48 69 String::String(const char* str, size_t n)
leothedragon 0:8f0bb79ddd48 70 {
leothedragon 0:8f0bb79ddd48 71 p = static_cast<char*>(malloc(n + 1));
leothedragon 0:8f0bb79ddd48 72
leothedragon 0:8f0bb79ddd48 73 allocated_ = n + 1;
leothedragon 0:8f0bb79ddd48 74 size_ = n;
leothedragon 0:8f0bb79ddd48 75 memcpy(p, str, n);
leothedragon 0:8f0bb79ddd48 76 p[n] = 0;
leothedragon 0:8f0bb79ddd48 77 }
leothedragon 0:8f0bb79ddd48 78
leothedragon 0:8f0bb79ddd48 79 String& String::operator=(const char* s)
leothedragon 0:8f0bb79ddd48 80 {
leothedragon 0:8f0bb79ddd48 81 if ( p != s ) {
leothedragon 0:8f0bb79ddd48 82 // s could point into our own string, so we have to allocate a new string
leothedragon 0:8f0bb79ddd48 83 const size_t len = strlen(s);
leothedragon 0:8f0bb79ddd48 84 char* copy = (char*) malloc( len + 1);
leothedragon 0:8f0bb79ddd48 85 memmove(copy, s, len+1); // trailing 0
leothedragon 0:8f0bb79ddd48 86 free( p );
leothedragon 0:8f0bb79ddd48 87 p = copy;
leothedragon 0:8f0bb79ddd48 88 size_ = len;
leothedragon 0:8f0bb79ddd48 89 allocated_ = len+1;
leothedragon 0:8f0bb79ddd48 90 }
leothedragon 0:8f0bb79ddd48 91 return *this;
leothedragon 0:8f0bb79ddd48 92 }
leothedragon 0:8f0bb79ddd48 93
leothedragon 0:8f0bb79ddd48 94 String& String::operator=(const String& s)
leothedragon 0:8f0bb79ddd48 95 {
leothedragon 0:8f0bb79ddd48 96 return operator=(s.p);
leothedragon 0:8f0bb79ddd48 97 }
leothedragon 0:8f0bb79ddd48 98
leothedragon 0:8f0bb79ddd48 99 String& String::operator+=(const String& s)
leothedragon 0:8f0bb79ddd48 100 {
leothedragon 0:8f0bb79ddd48 101 if (s.size_ > 0) {
leothedragon 0:8f0bb79ddd48 102 this->reserve(size_ + s.size_);
leothedragon 0:8f0bb79ddd48 103 memmove(p+size_, s.p, s.size_+1); // trailing 0
leothedragon 0:8f0bb79ddd48 104 size_ += s.size_;
leothedragon 0:8f0bb79ddd48 105 }
leothedragon 0:8f0bb79ddd48 106 return *this;
leothedragon 0:8f0bb79ddd48 107 }
leothedragon 0:8f0bb79ddd48 108
leothedragon 0:8f0bb79ddd48 109 // since p and s may overlap, we have to copy our own string first
leothedragon 0:8f0bb79ddd48 110 String& String::operator+=(const char* s)
leothedragon 0:8f0bb79ddd48 111 {
leothedragon 0:8f0bb79ddd48 112 const size_type lens = strlen(s);
leothedragon 0:8f0bb79ddd48 113 if (lens > 0) {
leothedragon 0:8f0bb79ddd48 114 if (size_ + lens + 1 <= allocated_) {
leothedragon 0:8f0bb79ddd48 115 memmove(p+size_, s, lens+1); // trailing 0
leothedragon 0:8f0bb79ddd48 116 size_ += lens;
leothedragon 0:8f0bb79ddd48 117 } else {
leothedragon 0:8f0bb79ddd48 118 String s2( *this ); // copy own data
leothedragon 0:8f0bb79ddd48 119 s2.reserve(size_ + lens);
leothedragon 0:8f0bb79ddd48 120 memmove(s2.p+size_, s, lens+1); // trailing 0
leothedragon 0:8f0bb79ddd48 121 s2.size_ = size_ + lens;
leothedragon 0:8f0bb79ddd48 122 this->swap( s2 );
leothedragon 0:8f0bb79ddd48 123 }
leothedragon 0:8f0bb79ddd48 124 }
leothedragon 0:8f0bb79ddd48 125 return *this;
leothedragon 0:8f0bb79ddd48 126 }
leothedragon 0:8f0bb79ddd48 127
leothedragon 0:8f0bb79ddd48 128 String& String::operator+=(const char c)
leothedragon 0:8f0bb79ddd48 129 {
leothedragon 0:8f0bb79ddd48 130 push_back(c);
leothedragon 0:8f0bb79ddd48 131 return *this;
leothedragon 0:8f0bb79ddd48 132 }
leothedragon 0:8f0bb79ddd48 133
leothedragon 0:8f0bb79ddd48 134 void String::push_back(const char c) {
leothedragon 0:8f0bb79ddd48 135
leothedragon 0:8f0bb79ddd48 136 if (size_ == allocated_ - 1) {
leothedragon 0:8f0bb79ddd48 137 size_t more = (allocated_* 3) / 2; // factor 1.5
leothedragon 0:8f0bb79ddd48 138 if ( more < 4 ) more = 4;
leothedragon 0:8f0bb79ddd48 139 reserve( size_ + more );
leothedragon 0:8f0bb79ddd48 140 }
leothedragon 0:8f0bb79ddd48 141
leothedragon 0:8f0bb79ddd48 142 p[size_] = c;
leothedragon 0:8f0bb79ddd48 143 size_++;
leothedragon 0:8f0bb79ddd48 144 p[size_] = 0;
leothedragon 0:8f0bb79ddd48 145 }
leothedragon 0:8f0bb79ddd48 146
leothedragon 0:8f0bb79ddd48 147 bool String::operator==(const char* s) const
leothedragon 0:8f0bb79ddd48 148 {
leothedragon 0:8f0bb79ddd48 149 if( s == NULL ) {
leothedragon 0:8f0bb79ddd48 150 if( p == NULL ) {
leothedragon 0:8f0bb79ddd48 151 return true;
leothedragon 0:8f0bb79ddd48 152 }
leothedragon 0:8f0bb79ddd48 153 return false;
leothedragon 0:8f0bb79ddd48 154 }
leothedragon 0:8f0bb79ddd48 155 bool ret = strcmp(p, s);
leothedragon 0:8f0bb79ddd48 156 return !ret;
leothedragon 0:8f0bb79ddd48 157 }
leothedragon 0:8f0bb79ddd48 158
leothedragon 0:8f0bb79ddd48 159 bool String::operator==(const String& s) const
leothedragon 0:8f0bb79ddd48 160 {
leothedragon 0:8f0bb79ddd48 161 bool ret = strcmp(p, s.p);
leothedragon 0:8f0bb79ddd48 162 return !ret;
leothedragon 0:8f0bb79ddd48 163 }
leothedragon 0:8f0bb79ddd48 164
leothedragon 0:8f0bb79ddd48 165 void String::clear()
leothedragon 0:8f0bb79ddd48 166 {
leothedragon 0:8f0bb79ddd48 167 size_ = 0;
leothedragon 0:8f0bb79ddd48 168 p[0] = 0;
leothedragon 0:8f0bb79ddd48 169 }
leothedragon 0:8f0bb79ddd48 170
leothedragon 0:8f0bb79ddd48 171 String String::substr(const size_type pos, size_type length) const
leothedragon 0:8f0bb79ddd48 172 {
leothedragon 0:8f0bb79ddd48 173 String s;
leothedragon 0:8f0bb79ddd48 174 const size_type len = size_;
leothedragon 0:8f0bb79ddd48 175
leothedragon 0:8f0bb79ddd48 176 if ( pos <= len ) {
leothedragon 0:8f0bb79ddd48 177
leothedragon 0:8f0bb79ddd48 178 size_type remain = len - pos;
leothedragon 0:8f0bb79ddd48 179
leothedragon 0:8f0bb79ddd48 180 if ( length > remain )
leothedragon 0:8f0bb79ddd48 181 length = remain;
leothedragon 0:8f0bb79ddd48 182
leothedragon 0:8f0bb79ddd48 183 s.reserve( length );
leothedragon 0:8f0bb79ddd48 184
leothedragon 0:8f0bb79ddd48 185 memcpy(s.p, p + pos, length);
leothedragon 0:8f0bb79ddd48 186 s.p[length] = '\0';
leothedragon 0:8f0bb79ddd48 187 s.size_ = length;
leothedragon 0:8f0bb79ddd48 188 }
leothedragon 0:8f0bb79ddd48 189 return s;
leothedragon 0:8f0bb79ddd48 190 }
leothedragon 0:8f0bb79ddd48 191
leothedragon 0:8f0bb79ddd48 192
leothedragon 0:8f0bb79ddd48 193 // checked access, accessing the NUL at end is allowed
leothedragon 0:8f0bb79ddd48 194 char String::at(const size_type i) const
leothedragon 0:8f0bb79ddd48 195 {
leothedragon 0:8f0bb79ddd48 196 if ( i <= strlen(p) ) {
leothedragon 0:8f0bb79ddd48 197 return p[i];
leothedragon 0:8f0bb79ddd48 198 } else {
leothedragon 0:8f0bb79ddd48 199 return '\0';
leothedragon 0:8f0bb79ddd48 200 }
leothedragon 0:8f0bb79ddd48 201 }
leothedragon 0:8f0bb79ddd48 202
leothedragon 0:8f0bb79ddd48 203 String& String::erase(size_type pos, size_type len)
leothedragon 0:8f0bb79ddd48 204 {
leothedragon 0:8f0bb79ddd48 205 if (len > 0) {
leothedragon 0:8f0bb79ddd48 206
leothedragon 0:8f0bb79ddd48 207 if ( pos < size_ ) { // user must not remove trailing 0
leothedragon 0:8f0bb79ddd48 208
leothedragon 0:8f0bb79ddd48 209 size_type s2 = size_;
leothedragon 0:8f0bb79ddd48 210 size_type remain = s2 - pos - len;
leothedragon 0:8f0bb79ddd48 211
leothedragon 0:8f0bb79ddd48 212 if (remain > 0) {
leothedragon 0:8f0bb79ddd48 213 // erase by overwriting
leothedragon 0:8f0bb79ddd48 214 memmove(p + pos, p + pos + len, remain);
leothedragon 0:8f0bb79ddd48 215 }
leothedragon 0:8f0bb79ddd48 216
leothedragon 0:8f0bb79ddd48 217 //if ( remain < 0 ) remain = 0;
leothedragon 0:8f0bb79ddd48 218
leothedragon 0:8f0bb79ddd48 219 // remove unused space
leothedragon 0:8f0bb79ddd48 220 this->resize( pos+remain );
leothedragon 0:8f0bb79ddd48 221
leothedragon 0:8f0bb79ddd48 222 }
leothedragon 0:8f0bb79ddd48 223 }
leothedragon 0:8f0bb79ddd48 224 return *this;
leothedragon 0:8f0bb79ddd48 225 }
leothedragon 0:8f0bb79ddd48 226
leothedragon 0:8f0bb79ddd48 227 String& String::append( const char* str, size_type n) {
leothedragon 0:8f0bb79ddd48 228 if (str && n > 0) {
leothedragon 0:8f0bb79ddd48 229 size_t lens = strlen(str);
leothedragon 0:8f0bb79ddd48 230 if (n > lens)
leothedragon 0:8f0bb79ddd48 231 n = lens;
leothedragon 0:8f0bb79ddd48 232 size_t newlen = size_ + n;
leothedragon 0:8f0bb79ddd48 233 this->reserve( newlen );
leothedragon 0:8f0bb79ddd48 234 memmove(p+size_, str, n); // p and s.p MAY overlap
leothedragon 0:8f0bb79ddd48 235 p[newlen] = 0; // add NUL termination
leothedragon 0:8f0bb79ddd48 236 size_ = newlen;
leothedragon 0:8f0bb79ddd48 237 }
leothedragon 0:8f0bb79ddd48 238 return *this;
leothedragon 0:8f0bb79ddd48 239 }
leothedragon 0:8f0bb79ddd48 240
leothedragon 0:8f0bb79ddd48 241 String& String::append_raw( const char* str, size_type n) {
leothedragon 0:8f0bb79ddd48 242 if (str && n > 0) {
leothedragon 0:8f0bb79ddd48 243 size_t newlen = size_ + n;
leothedragon 0:8f0bb79ddd48 244 this->reserve( newlen );
leothedragon 0:8f0bb79ddd48 245 memmove(p+size_, str, n); // p and s.p MAY overlap
leothedragon 0:8f0bb79ddd48 246 p[newlen] = 0; // add NUL termination
leothedragon 0:8f0bb79ddd48 247 size_ = newlen;
leothedragon 0:8f0bb79ddd48 248 }
leothedragon 0:8f0bb79ddd48 249 return *this;
leothedragon 0:8f0bb79ddd48 250 }
leothedragon 0:8f0bb79ddd48 251
leothedragon 0:8f0bb79ddd48 252 void String::append_int(int param) {
leothedragon 0:8f0bb79ddd48 253
leothedragon 0:8f0bb79ddd48 254 // max len of "-9223372036854775808" plus zero termination
leothedragon 0:8f0bb79ddd48 255 char conv_buff[20+1];
leothedragon 0:8f0bb79ddd48 256
leothedragon 0:8f0bb79ddd48 257 int len = itoa_c(param, conv_buff);
leothedragon 0:8f0bb79ddd48 258 append_raw(conv_buff, len);
leothedragon 0:8f0bb79ddd48 259 }
leothedragon 0:8f0bb79ddd48 260
leothedragon 0:8f0bb79ddd48 261 int String::compare( size_type pos, size_type len, const String& str ) const {
leothedragon 0:8f0bb79ddd48 262 int r = -1;
leothedragon 0:8f0bb79ddd48 263 if (pos <= size_) {
leothedragon 0:8f0bb79ddd48 264 if ( len > size_ - pos)
leothedragon 0:8f0bb79ddd48 265 len = size_ - pos; // limit len to available length
leothedragon 0:8f0bb79ddd48 266
leothedragon 0:8f0bb79ddd48 267 const size_type osize = str.size();
leothedragon 0:8f0bb79ddd48 268 const size_type len2 = MIN(len, osize);
leothedragon 0:8f0bb79ddd48 269 r = strncmp( p + pos, str.p, len2);
leothedragon 0:8f0bb79ddd48 270 if (r==0) // equal so far, now compare sizes
leothedragon 0:8f0bb79ddd48 271 r = len < osize ? -1 : ( len == osize ? 0 : +1 );
leothedragon 0:8f0bb79ddd48 272 }
leothedragon 0:8f0bb79ddd48 273 return r;
leothedragon 0:8f0bb79ddd48 274 }
leothedragon 0:8f0bb79ddd48 275
leothedragon 0:8f0bb79ddd48 276 int String::compare( size_type pos, size_type len, const char* str ) const {
leothedragon 0:8f0bb79ddd48 277 int r = -1;
leothedragon 0:8f0bb79ddd48 278 if (pos <= size_) {
leothedragon 0:8f0bb79ddd48 279
leothedragon 0:8f0bb79ddd48 280 if ( len > size_ - pos)
leothedragon 0:8f0bb79ddd48 281 len = size_ - pos; // limit len to available length
leothedragon 0:8f0bb79ddd48 282
leothedragon 0:8f0bb79ddd48 283 const size_type osize = strlen(str);
leothedragon 0:8f0bb79ddd48 284 const size_type len2 = MIN(len, osize);
leothedragon 0:8f0bb79ddd48 285 r = strncmp( p + pos, str, len2);
leothedragon 0:8f0bb79ddd48 286 if (r==0) // equal so far, now compare sizes
leothedragon 0:8f0bb79ddd48 287 r = len < osize ? -1 : ( len == osize ? 0 : +1 );
leothedragon 0:8f0bb79ddd48 288 }
leothedragon 0:8f0bb79ddd48 289 return r;
leothedragon 0:8f0bb79ddd48 290 }
leothedragon 0:8f0bb79ddd48 291
leothedragon 0:8f0bb79ddd48 292 int String::find_last_of(char c) const {
leothedragon 0:8f0bb79ddd48 293 int r = -1;
leothedragon 0:8f0bb79ddd48 294 char *v;
leothedragon 0:8f0bb79ddd48 295 v = strrchr(p,c);
leothedragon 0:8f0bb79ddd48 296 if (v != NULL) {
leothedragon 0:8f0bb79ddd48 297 r = 0;
leothedragon 0:8f0bb79ddd48 298 char* i = p;
leothedragon 0:8f0bb79ddd48 299 while (v != i) {
leothedragon 0:8f0bb79ddd48 300 i++;
leothedragon 0:8f0bb79ddd48 301 r++;
leothedragon 0:8f0bb79ddd48 302 }
leothedragon 0:8f0bb79ddd48 303 }
leothedragon 0:8f0bb79ddd48 304 return r;
leothedragon 0:8f0bb79ddd48 305 }
leothedragon 0:8f0bb79ddd48 306
leothedragon 0:8f0bb79ddd48 307 void String::new_realloc( size_type n) {
leothedragon 0:8f0bb79ddd48 308 if (n > 0 ) {
leothedragon 0:8f0bb79ddd48 309 char* pnew = static_cast<char*>(realloc(p, n)); // could return NULL
leothedragon 0:8f0bb79ddd48 310 if (pnew)
leothedragon 0:8f0bb79ddd48 311 p = pnew;
leothedragon 0:8f0bb79ddd48 312 }
leothedragon 0:8f0bb79ddd48 313 }
leothedragon 0:8f0bb79ddd48 314
leothedragon 0:8f0bb79ddd48 315 void String::reserve( const size_type n) {
leothedragon 0:8f0bb79ddd48 316 if (n >= allocated_ ) {
leothedragon 0:8f0bb79ddd48 317 this->new_realloc(n + 1);
leothedragon 0:8f0bb79ddd48 318 allocated_ = n + 1;
leothedragon 0:8f0bb79ddd48 319 }
leothedragon 0:8f0bb79ddd48 320 }
leothedragon 0:8f0bb79ddd48 321
leothedragon 0:8f0bb79ddd48 322 void String::resize( const size_type n) {
leothedragon 0:8f0bb79ddd48 323 this->resize( n, 0 );
leothedragon 0:8f0bb79ddd48 324 }
leothedragon 0:8f0bb79ddd48 325
leothedragon 0:8f0bb79ddd48 326 void String::resize( const size_type n, const char c) {
leothedragon 0:8f0bb79ddd48 327 if (n < size_ ) {
leothedragon 0:8f0bb79ddd48 328 p[n] = 0;
leothedragon 0:8f0bb79ddd48 329 size_ = n;
leothedragon 0:8f0bb79ddd48 330 }
leothedragon 0:8f0bb79ddd48 331 else if (n > size_ ) {
leothedragon 0:8f0bb79ddd48 332 this->reserve( n );
leothedragon 0:8f0bb79ddd48 333 for (size_type i=size_; i < n; ++i )
leothedragon 0:8f0bb79ddd48 334 p[i] = c;
leothedragon 0:8f0bb79ddd48 335 p[n] = 0;
leothedragon 0:8f0bb79ddd48 336 size_ = n;
leothedragon 0:8f0bb79ddd48 337 }
leothedragon 0:8f0bb79ddd48 338 }
leothedragon 0:8f0bb79ddd48 339
leothedragon 0:8f0bb79ddd48 340 void String::swap(String& s) {
leothedragon 0:8f0bb79ddd48 341
leothedragon 0:8f0bb79ddd48 342 // do the swap manually, without relience on std::swap() as that is not always available
leothedragon 0:8f0bb79ddd48 343 size_t temp;
leothedragon 0:8f0bb79ddd48 344 char* tempPtr;
leothedragon 0:8f0bb79ddd48 345
leothedragon 0:8f0bb79ddd48 346 temp = allocated_;
leothedragon 0:8f0bb79ddd48 347 allocated_ = s.allocated_;
leothedragon 0:8f0bb79ddd48 348 s.allocated_ = temp;
leothedragon 0:8f0bb79ddd48 349
leothedragon 0:8f0bb79ddd48 350 temp = size_;
leothedragon 0:8f0bb79ddd48 351 size_ = s.size_;
leothedragon 0:8f0bb79ddd48 352 s.size_ = temp;
leothedragon 0:8f0bb79ddd48 353
leothedragon 0:8f0bb79ddd48 354 tempPtr = p;
leothedragon 0:8f0bb79ddd48 355 p = s.p;
leothedragon 0:8f0bb79ddd48 356 s.p = tempPtr;
leothedragon 0:8f0bb79ddd48 357 }
leothedragon 0:8f0bb79ddd48 358
leothedragon 0:8f0bb79ddd48 359
leothedragon 0:8f0bb79ddd48 360 // Comparison
leothedragon 0:8f0bb79ddd48 361 bool operator<( const String& s1, const String& s2 ) {
leothedragon 0:8f0bb79ddd48 362 return strcmp( s1.c_str(), s2.c_str() ) < 0;
leothedragon 0:8f0bb79ddd48 363 }
leothedragon 0:8f0bb79ddd48 364
leothedragon 0:8f0bb79ddd48 365 void reverse(char s[], uint32_t length)
leothedragon 0:8f0bb79ddd48 366 {
leothedragon 0:8f0bb79ddd48 367 uint32_t i, j;
leothedragon 0:8f0bb79ddd48 368 char c;
leothedragon 0:8f0bb79ddd48 369
leothedragon 0:8f0bb79ddd48 370 for (i = 0, j = length-1; i<j; i++, j--) {
leothedragon 0:8f0bb79ddd48 371 c = s[i];
leothedragon 0:8f0bb79ddd48 372 s[i] = s[j];
leothedragon 0:8f0bb79ddd48 373 s[j] = c;
leothedragon 0:8f0bb79ddd48 374 }
leothedragon 0:8f0bb79ddd48 375 }
leothedragon 0:8f0bb79ddd48 376
leothedragon 0:8f0bb79ddd48 377 uint32_t itoa_c (int64_t n, char s[])
leothedragon 0:8f0bb79ddd48 378 {
leothedragon 0:8f0bb79ddd48 379 int64_t sign;
leothedragon 0:8f0bb79ddd48 380 uint32_t i;
leothedragon 0:8f0bb79ddd48 381
leothedragon 0:8f0bb79ddd48 382 if ((sign = n) < 0)
leothedragon 0:8f0bb79ddd48 383 n = -n;
leothedragon 0:8f0bb79ddd48 384
leothedragon 0:8f0bb79ddd48 385 i = 0;
leothedragon 0:8f0bb79ddd48 386
leothedragon 0:8f0bb79ddd48 387 do {
leothedragon 0:8f0bb79ddd48 388 s[i++] = n % 10 + '0';
leothedragon 0:8f0bb79ddd48 389 }
leothedragon 0:8f0bb79ddd48 390 while ((n /= 10) > 0);
leothedragon 0:8f0bb79ddd48 391
leothedragon 0:8f0bb79ddd48 392 if (sign < 0)
leothedragon 0:8f0bb79ddd48 393 s[i++] = '-';
leothedragon 0:8f0bb79ddd48 394
leothedragon 0:8f0bb79ddd48 395 s[i] = '\0';
leothedragon 0:8f0bb79ddd48 396
leothedragon 0:8f0bb79ddd48 397 m2m::reverse(s, i);
leothedragon 0:8f0bb79ddd48 398 return i;
leothedragon 0:8f0bb79ddd48 399 }
leothedragon 0:8f0bb79ddd48 400
leothedragon 0:8f0bb79ddd48 401 uint8_t* String::convert_integer_to_array(int64_t value, uint8_t &size, const uint8_t *array, const uint32_t array_size)
leothedragon 0:8f0bb79ddd48 402 {
leothedragon 0:8f0bb79ddd48 403 uint8_t* buffer = NULL;
leothedragon 0:8f0bb79ddd48 404 size = 0;
leothedragon 0:8f0bb79ddd48 405 if (array) {
leothedragon 0:8f0bb79ddd48 406 value = String::convert_array_to_integer(array, array_size);
leothedragon 0:8f0bb79ddd48 407 }
leothedragon 0:8f0bb79ddd48 408
leothedragon 0:8f0bb79ddd48 409 if(value < 0xFF) {
leothedragon 0:8f0bb79ddd48 410 size = 1;
leothedragon 0:8f0bb79ddd48 411 } else if(value < 0xFFFF) {
leothedragon 0:8f0bb79ddd48 412 size = 2;
leothedragon 0:8f0bb79ddd48 413 } else if(value < 0xFFFFFF) {
leothedragon 0:8f0bb79ddd48 414 size = 3;
leothedragon 0:8f0bb79ddd48 415 } else if(value < 0xFFFFFFFF) {
leothedragon 0:8f0bb79ddd48 416 size = 4;
leothedragon 0:8f0bb79ddd48 417 } else if(value < 0xFFFFFFFFFF) {
leothedragon 0:8f0bb79ddd48 418 size = 5;
leothedragon 0:8f0bb79ddd48 419 } else if(value < 0xFFFFFFFFFFFF) {
leothedragon 0:8f0bb79ddd48 420 size = 6;
leothedragon 0:8f0bb79ddd48 421 } else if(value < 0xFFFFFFFFFFFFFF) {
leothedragon 0:8f0bb79ddd48 422 size = 7;
leothedragon 0:8f0bb79ddd48 423 } else {
leothedragon 0:8f0bb79ddd48 424 size = 8;
leothedragon 0:8f0bb79ddd48 425 }
leothedragon 0:8f0bb79ddd48 426
leothedragon 0:8f0bb79ddd48 427 buffer = (uint8_t*)malloc(size);
leothedragon 0:8f0bb79ddd48 428 if (buffer) {
leothedragon 0:8f0bb79ddd48 429 for (int i = 0; i < size; i++) {
leothedragon 0:8f0bb79ddd48 430 buffer[i] = (value >> ((size - i - 1) * 8));
leothedragon 0:8f0bb79ddd48 431 }
leothedragon 0:8f0bb79ddd48 432 } else {
leothedragon 0:8f0bb79ddd48 433 size = 0;
leothedragon 0:8f0bb79ddd48 434 }
leothedragon 0:8f0bb79ddd48 435 return buffer;
leothedragon 0:8f0bb79ddd48 436 }
leothedragon 0:8f0bb79ddd48 437
leothedragon 0:8f0bb79ddd48 438 int64_t String::convert_array_to_integer(const uint8_t *value, const uint32_t size)
leothedragon 0:8f0bb79ddd48 439 {
leothedragon 0:8f0bb79ddd48 440 int64_t temp_64 = 0;
leothedragon 0:8f0bb79ddd48 441 for (int i = size - 1; i >= 0; i--) {
leothedragon 0:8f0bb79ddd48 442 temp_64 += (uint64_t)(*value++) << i * 8;
leothedragon 0:8f0bb79ddd48 443 }
leothedragon 0:8f0bb79ddd48 444 return temp_64;
leothedragon 0:8f0bb79ddd48 445 }
leothedragon 0:8f0bb79ddd48 446
leothedragon 0:8f0bb79ddd48 447 bool String::convert_ascii_to_int(const char *value, size_t length, int64_t &conversion_result)
leothedragon 0:8f0bb79ddd48 448 {
leothedragon 0:8f0bb79ddd48 449 unsigned int index = 0;
leothedragon 0:8f0bb79ddd48 450
leothedragon 0:8f0bb79ddd48 451 int64_t result = 0;
leothedragon 0:8f0bb79ddd48 452
leothedragon 0:8f0bb79ddd48 453 int sign;
leothedragon 0:8f0bb79ddd48 454
leothedragon 0:8f0bb79ddd48 455 int digit;
leothedragon 0:8f0bb79ddd48 456
leothedragon 0:8f0bb79ddd48 457 // return value, will be set to true if at least one digit is found
leothedragon 0:8f0bb79ddd48 458 // and a false is returned if
leothedragon 0:8f0bb79ddd48 459 // a) no digits found, ie. string is empty
leothedragon 0:8f0bb79ddd48 460 // b) a non-digit or non-'+' or non-'-' char is found.
leothedragon 0:8f0bb79ddd48 461 // c) more than one +, - chars are found
leothedragon 0:8f0bb79ddd48 462 //
leothedragon 0:8f0bb79ddd48 463 bool success = false;
leothedragon 0:8f0bb79ddd48 464
leothedragon 0:8f0bb79ddd48 465 // have predictable output value even on error
leothedragon 0:8f0bb79ddd48 466 conversion_result = 0;
leothedragon 0:8f0bb79ddd48 467
leothedragon 0:8f0bb79ddd48 468 // the optional sign needs to be the first char
leothedragon 0:8f0bb79ddd48 469 if ((length > 0) && (value[index] == '+')) {
leothedragon 0:8f0bb79ddd48 470 sign = 1;
leothedragon 0:8f0bb79ddd48 471 index++;
leothedragon 0:8f0bb79ddd48 472 } else if ((length > 0) && (value[index] == '-')) {
leothedragon 0:8f0bb79ddd48 473 sign = -1;
leothedragon 0:8f0bb79ddd48 474 index++;
leothedragon 0:8f0bb79ddd48 475 } else {
leothedragon 0:8f0bb79ddd48 476 sign = 1;
leothedragon 0:8f0bb79ddd48 477 }
leothedragon 0:8f0bb79ddd48 478
leothedragon 0:8f0bb79ddd48 479 while ((index < length) && (value[index] != 0)) {
leothedragon 0:8f0bb79ddd48 480
leothedragon 0:8f0bb79ddd48 481 const char c = value[index++];
leothedragon 0:8f0bb79ddd48 482
leothedragon 0:8f0bb79ddd48 483 switch (c) {
leothedragon 0:8f0bb79ddd48 484
leothedragon 0:8f0bb79ddd48 485 case '0':
leothedragon 0:8f0bb79ddd48 486 case '1':
leothedragon 0:8f0bb79ddd48 487 case '2':
leothedragon 0:8f0bb79ddd48 488 case '3':
leothedragon 0:8f0bb79ddd48 489 case '4':
leothedragon 0:8f0bb79ddd48 490 case '5':
leothedragon 0:8f0bb79ddd48 491 case '6':
leothedragon 0:8f0bb79ddd48 492 case '7':
leothedragon 0:8f0bb79ddd48 493 case '8':
leothedragon 0:8f0bb79ddd48 494 case '9':
leothedragon 0:8f0bb79ddd48 495 digit = c - '0';
leothedragon 0:8f0bb79ddd48 496 result *= 10;
leothedragon 0:8f0bb79ddd48 497 result += digit;
leothedragon 0:8f0bb79ddd48 498 success = true; // at least one digit was converted successfully
leothedragon 0:8f0bb79ddd48 499 break;
leothedragon 0:8f0bb79ddd48 500
leothedragon 0:8f0bb79ddd48 501 // there can be only one sign char and it must be the first
leothedragon 0:8f0bb79ddd48 502 case '+':
leothedragon 0:8f0bb79ddd48 503 case '-':
leothedragon 0:8f0bb79ddd48 504 default:
leothedragon 0:8f0bb79ddd48 505 // Note: the handling of having a number with digits in front and
leothedragon 0:8f0bb79ddd48 506 // non-digits at end (eg. "0zero") differs from sscanf() on glibc,
leothedragon 0:8f0bb79ddd48 507 // as sscanf will return what it got converted and a success value
leothedragon 0:8f0bb79ddd48 508 // even if the string ended with junk.
leothedragon 0:8f0bb79ddd48 509 conversion_result = 0;
leothedragon 0:8f0bb79ddd48 510 return false;
leothedragon 0:8f0bb79ddd48 511 }
leothedragon 0:8f0bb79ddd48 512 }
leothedragon 0:8f0bb79ddd48 513
leothedragon 0:8f0bb79ddd48 514 // put the sign in place
leothedragon 0:8f0bb79ddd48 515 result *= sign;
leothedragon 0:8f0bb79ddd48 516
leothedragon 0:8f0bb79ddd48 517 // and pass the result to caller
leothedragon 0:8f0bb79ddd48 518 conversion_result = result;
leothedragon 0:8f0bb79ddd48 519
leothedragon 0:8f0bb79ddd48 520 return success;
leothedragon 0:8f0bb79ddd48 521 }
leothedragon 0:8f0bb79ddd48 522
leothedragon 0:8f0bb79ddd48 523 } // namespace