Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16 #include "mbed-client/m2mstring.h"
nexpaq 0:6c56fb4bc5f0 17 #include <string.h> // strlen
nexpaq 0:6c56fb4bc5f0 18 #include <stdlib.h> // malloc, realloc
nexpaq 0:6c56fb4bc5f0 19 #include <assert.h>
nexpaq 0:6c56fb4bc5f0 20 #include <algorithm> // min
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 namespace m2m {
nexpaq 0:6c56fb4bc5f0 23
nexpaq 0:6c56fb4bc5f0 24 const String::size_type String::npos = static_cast<size_t>(-1);
nexpaq 0:6c56fb4bc5f0 25
nexpaq 0:6c56fb4bc5f0 26 char* String::strdup(const char* s)
nexpaq 0:6c56fb4bc5f0 27 {
nexpaq 0:6c56fb4bc5f0 28 const size_t len = strlen(s)+1;
nexpaq 0:6c56fb4bc5f0 29 char *p2 = static_cast<char*>(malloc(len));
nexpaq 0:6c56fb4bc5f0 30 memcpy(p2, s, len);
nexpaq 0:6c56fb4bc5f0 31 allocated_ = len;
nexpaq 0:6c56fb4bc5f0 32 size_ = len-1;
nexpaq 0:6c56fb4bc5f0 33 return p2;
nexpaq 0:6c56fb4bc5f0 34 }
nexpaq 0:6c56fb4bc5f0 35
nexpaq 0:6c56fb4bc5f0 36 String::String()
nexpaq 0:6c56fb4bc5f0 37 : p( strdup("") )
nexpaq 0:6c56fb4bc5f0 38 {
nexpaq 0:6c56fb4bc5f0 39 }
nexpaq 0:6c56fb4bc5f0 40
nexpaq 0:6c56fb4bc5f0 41 String::~String()
nexpaq 0:6c56fb4bc5f0 42 {
nexpaq 0:6c56fb4bc5f0 43 free(p);
nexpaq 0:6c56fb4bc5f0 44 p = 0;
nexpaq 0:6c56fb4bc5f0 45 }
nexpaq 0:6c56fb4bc5f0 46
nexpaq 0:6c56fb4bc5f0 47 String::String(const String& s)
nexpaq 0:6c56fb4bc5f0 48 : p(0)
nexpaq 0:6c56fb4bc5f0 49 {
nexpaq 0:6c56fb4bc5f0 50 if( &s != NULL ) {
nexpaq 0:6c56fb4bc5f0 51 p = static_cast<char*>(malloc(s.size_ + 1));
nexpaq 0:6c56fb4bc5f0 52
nexpaq 0:6c56fb4bc5f0 53 allocated_ = s.size_ + 1;
nexpaq 0:6c56fb4bc5f0 54 size_ = s.size_;
nexpaq 0:6c56fb4bc5f0 55 memcpy(p, s.p, size_ + 1);
nexpaq 0:6c56fb4bc5f0 56 }
nexpaq 0:6c56fb4bc5f0 57 }
nexpaq 0:6c56fb4bc5f0 58
nexpaq 0:6c56fb4bc5f0 59 String::String(const char* s)
nexpaq 0:6c56fb4bc5f0 60 : p(strdup(s))
nexpaq 0:6c56fb4bc5f0 61 {
nexpaq 0:6c56fb4bc5f0 62 }
nexpaq 0:6c56fb4bc5f0 63
nexpaq 0:6c56fb4bc5f0 64 String& String::operator=(const char* s)
nexpaq 0:6c56fb4bc5f0 65 {
nexpaq 0:6c56fb4bc5f0 66 if ( p != s ) {
nexpaq 0:6c56fb4bc5f0 67 // s could point into our own string, so we have to allocate a new string
nexpaq 0:6c56fb4bc5f0 68 const size_t len = strlen(s);
nexpaq 0:6c56fb4bc5f0 69 char* copy = (char*) malloc( len + 1);
nexpaq 0:6c56fb4bc5f0 70 memmove(copy, s, len+1); // trailing 0
nexpaq 0:6c56fb4bc5f0 71 free( p );
nexpaq 0:6c56fb4bc5f0 72 p = copy;
nexpaq 0:6c56fb4bc5f0 73 size_ = len;
nexpaq 0:6c56fb4bc5f0 74 allocated_ = len+1;
nexpaq 0:6c56fb4bc5f0 75 }
nexpaq 0:6c56fb4bc5f0 76 return *this;
nexpaq 0:6c56fb4bc5f0 77 }
nexpaq 0:6c56fb4bc5f0 78
nexpaq 0:6c56fb4bc5f0 79 String& String::operator=(const String& s)
nexpaq 0:6c56fb4bc5f0 80 {
nexpaq 0:6c56fb4bc5f0 81 return operator=(s.p);
nexpaq 0:6c56fb4bc5f0 82 }
nexpaq 0:6c56fb4bc5f0 83
nexpaq 0:6c56fb4bc5f0 84 String& String::operator+=(const String& s)
nexpaq 0:6c56fb4bc5f0 85 {
nexpaq 0:6c56fb4bc5f0 86 if (s.size_ > 0) {
nexpaq 0:6c56fb4bc5f0 87 this->reserve(size_ + s.size_);
nexpaq 0:6c56fb4bc5f0 88 memmove(p+size_, s.p, s.size_+1); // trailing 0
nexpaq 0:6c56fb4bc5f0 89 size_ += s.size_;
nexpaq 0:6c56fb4bc5f0 90 }
nexpaq 0:6c56fb4bc5f0 91 return *this;
nexpaq 0:6c56fb4bc5f0 92 }
nexpaq 0:6c56fb4bc5f0 93
nexpaq 0:6c56fb4bc5f0 94 // since p and s may overlap, we have to copy our own string first
nexpaq 0:6c56fb4bc5f0 95 String& String::operator+=(const char* s)
nexpaq 0:6c56fb4bc5f0 96 {
nexpaq 0:6c56fb4bc5f0 97 const size_type lens = strlen(s);
nexpaq 0:6c56fb4bc5f0 98 if (lens > 0) {
nexpaq 0:6c56fb4bc5f0 99 if (size_ + lens + 1 <= allocated_) {
nexpaq 0:6c56fb4bc5f0 100 memmove(p+size_, s, lens+1); // trailing 0
nexpaq 0:6c56fb4bc5f0 101 size_ += lens;
nexpaq 0:6c56fb4bc5f0 102 } else {
nexpaq 0:6c56fb4bc5f0 103 String s2( *this ); // copy own data
nexpaq 0:6c56fb4bc5f0 104 s2.reserve(size_ + lens);
nexpaq 0:6c56fb4bc5f0 105 memmove(s2.p+size_, s, lens+1); // trailing 0
nexpaq 0:6c56fb4bc5f0 106 s2.size_ = size_ + lens;
nexpaq 0:6c56fb4bc5f0 107 this->swap( s2 );
nexpaq 0:6c56fb4bc5f0 108 }
nexpaq 0:6c56fb4bc5f0 109 }
nexpaq 0:6c56fb4bc5f0 110 return *this;
nexpaq 0:6c56fb4bc5f0 111 }
nexpaq 0:6c56fb4bc5f0 112
nexpaq 0:6c56fb4bc5f0 113 String& String::operator+=(const char c)
nexpaq 0:6c56fb4bc5f0 114 {
nexpaq 0:6c56fb4bc5f0 115 push_back(c);
nexpaq 0:6c56fb4bc5f0 116 return *this;
nexpaq 0:6c56fb4bc5f0 117 }
nexpaq 0:6c56fb4bc5f0 118
nexpaq 0:6c56fb4bc5f0 119 void String::push_back(const char c) {
nexpaq 0:6c56fb4bc5f0 120
nexpaq 0:6c56fb4bc5f0 121 if (size_ == allocated_ - 1) {
nexpaq 0:6c56fb4bc5f0 122 size_t more = (allocated_* 3) / 2; // factor 1.5
nexpaq 0:6c56fb4bc5f0 123 if ( more < 4 ) more = 4;
nexpaq 0:6c56fb4bc5f0 124 reserve( size_ + more );
nexpaq 0:6c56fb4bc5f0 125 }
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127 p[size_] = c;
nexpaq 0:6c56fb4bc5f0 128 size_++;
nexpaq 0:6c56fb4bc5f0 129 p[size_] = 0;
nexpaq 0:6c56fb4bc5f0 130 }
nexpaq 0:6c56fb4bc5f0 131
nexpaq 0:6c56fb4bc5f0 132 bool String::operator==(const char* s) const
nexpaq 0:6c56fb4bc5f0 133 {
nexpaq 0:6c56fb4bc5f0 134 if( s == NULL ) {
nexpaq 0:6c56fb4bc5f0 135 if( p == NULL ) {
nexpaq 0:6c56fb4bc5f0 136 return true;
nexpaq 0:6c56fb4bc5f0 137 }
nexpaq 0:6c56fb4bc5f0 138 return false;
nexpaq 0:6c56fb4bc5f0 139 }
nexpaq 0:6c56fb4bc5f0 140 bool ret = strcmp(p, s);
nexpaq 0:6c56fb4bc5f0 141 return !ret;
nexpaq 0:6c56fb4bc5f0 142 }
nexpaq 0:6c56fb4bc5f0 143
nexpaq 0:6c56fb4bc5f0 144 bool String::operator==(const String& s) const
nexpaq 0:6c56fb4bc5f0 145 {
nexpaq 0:6c56fb4bc5f0 146 bool ret = strcmp(p, s.p);
nexpaq 0:6c56fb4bc5f0 147 return !ret;
nexpaq 0:6c56fb4bc5f0 148 }
nexpaq 0:6c56fb4bc5f0 149
nexpaq 0:6c56fb4bc5f0 150 void String::clear()
nexpaq 0:6c56fb4bc5f0 151 {
nexpaq 0:6c56fb4bc5f0 152 size_ = 0;
nexpaq 0:6c56fb4bc5f0 153 p[0] = 0;
nexpaq 0:6c56fb4bc5f0 154 }
nexpaq 0:6c56fb4bc5f0 155
nexpaq 0:6c56fb4bc5f0 156 String String::substr(const size_type pos, size_type length) const
nexpaq 0:6c56fb4bc5f0 157 {
nexpaq 0:6c56fb4bc5f0 158 String s;
nexpaq 0:6c56fb4bc5f0 159 const size_type len = size_;
nexpaq 0:6c56fb4bc5f0 160
nexpaq 0:6c56fb4bc5f0 161 if ( pos <= len ) {
nexpaq 0:6c56fb4bc5f0 162
nexpaq 0:6c56fb4bc5f0 163 size_type remain = len - pos;
nexpaq 0:6c56fb4bc5f0 164
nexpaq 0:6c56fb4bc5f0 165 if ( length > remain )
nexpaq 0:6c56fb4bc5f0 166 length = remain;
nexpaq 0:6c56fb4bc5f0 167
nexpaq 0:6c56fb4bc5f0 168 s.reserve( length );
nexpaq 0:6c56fb4bc5f0 169
nexpaq 0:6c56fb4bc5f0 170 memcpy(s.p, p + pos, length);
nexpaq 0:6c56fb4bc5f0 171 s.p[length] = '\0';
nexpaq 0:6c56fb4bc5f0 172 s.size_ = length;
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174 return s;
nexpaq 0:6c56fb4bc5f0 175 }
nexpaq 0:6c56fb4bc5f0 176
nexpaq 0:6c56fb4bc5f0 177
nexpaq 0:6c56fb4bc5f0 178 // checked access, accessing the NUL at end is allowed
nexpaq 0:6c56fb4bc5f0 179 char String::at(const size_type i) const
nexpaq 0:6c56fb4bc5f0 180 {
nexpaq 0:6c56fb4bc5f0 181 if ( i <= strlen(p) ) {
nexpaq 0:6c56fb4bc5f0 182 return p[i];
nexpaq 0:6c56fb4bc5f0 183 } else {
nexpaq 0:6c56fb4bc5f0 184 return '\0';
nexpaq 0:6c56fb4bc5f0 185 }
nexpaq 0:6c56fb4bc5f0 186 }
nexpaq 0:6c56fb4bc5f0 187
nexpaq 0:6c56fb4bc5f0 188 String& String::erase(size_type pos, size_type len)
nexpaq 0:6c56fb4bc5f0 189 {
nexpaq 0:6c56fb4bc5f0 190 if (len > 0) {
nexpaq 0:6c56fb4bc5f0 191
nexpaq 0:6c56fb4bc5f0 192 if ( pos < size_ ) { // user must not remove trailing 0
nexpaq 0:6c56fb4bc5f0 193
nexpaq 0:6c56fb4bc5f0 194 size_type s2 = size_;
nexpaq 0:6c56fb4bc5f0 195 size_type remain = s2 - pos - len;
nexpaq 0:6c56fb4bc5f0 196
nexpaq 0:6c56fb4bc5f0 197 if (remain > 0) {
nexpaq 0:6c56fb4bc5f0 198 // erase by overwriting
nexpaq 0:6c56fb4bc5f0 199 memmove(p + pos, p + pos + len, remain);
nexpaq 0:6c56fb4bc5f0 200 }
nexpaq 0:6c56fb4bc5f0 201
nexpaq 0:6c56fb4bc5f0 202 //if ( remain < 0 ) remain = 0;
nexpaq 0:6c56fb4bc5f0 203
nexpaq 0:6c56fb4bc5f0 204 // remove unused space
nexpaq 0:6c56fb4bc5f0 205 this->resize( pos+remain );
nexpaq 0:6c56fb4bc5f0 206
nexpaq 0:6c56fb4bc5f0 207 }
nexpaq 0:6c56fb4bc5f0 208 }
nexpaq 0:6c56fb4bc5f0 209 return *this;
nexpaq 0:6c56fb4bc5f0 210 }
nexpaq 0:6c56fb4bc5f0 211
nexpaq 0:6c56fb4bc5f0 212 String& String::append( const char* str, size_type n) {
nexpaq 0:6c56fb4bc5f0 213 if (str && n > 0) {
nexpaq 0:6c56fb4bc5f0 214 size_t lens = strlen(str);
nexpaq 0:6c56fb4bc5f0 215 if (n > lens)
nexpaq 0:6c56fb4bc5f0 216 n = lens;
nexpaq 0:6c56fb4bc5f0 217 size_t newlen = size_ + n;
nexpaq 0:6c56fb4bc5f0 218 this->reserve( newlen );
nexpaq 0:6c56fb4bc5f0 219 memmove(p+size_, str, n); // p and s.p MAY overlap
nexpaq 0:6c56fb4bc5f0 220 p[newlen] = 0; // add NUL termination
nexpaq 0:6c56fb4bc5f0 221 size_ = newlen;
nexpaq 0:6c56fb4bc5f0 222 }
nexpaq 0:6c56fb4bc5f0 223 return *this;
nexpaq 0:6c56fb4bc5f0 224 }
nexpaq 0:6c56fb4bc5f0 225
nexpaq 0:6c56fb4bc5f0 226 String& String::append_raw( const char* str, size_type n) {
nexpaq 0:6c56fb4bc5f0 227 if (str && n > 0) {
nexpaq 0:6c56fb4bc5f0 228 size_t newlen = size_ + n;
nexpaq 0:6c56fb4bc5f0 229 this->reserve( newlen );
nexpaq 0:6c56fb4bc5f0 230 memmove(p+size_, str, n); // p and s.p MAY overlap
nexpaq 0:6c56fb4bc5f0 231 p[newlen] = 0; // add NUL termination
nexpaq 0:6c56fb4bc5f0 232 size_ = newlen;
nexpaq 0:6c56fb4bc5f0 233 }
nexpaq 0:6c56fb4bc5f0 234 return *this;
nexpaq 0:6c56fb4bc5f0 235 }
nexpaq 0:6c56fb4bc5f0 236
nexpaq 0:6c56fb4bc5f0 237 void String::append_int(int param) {
nexpaq 0:6c56fb4bc5f0 238
nexpaq 0:6c56fb4bc5f0 239 // max len of "-9223372036854775808" plus zero termination
nexpaq 0:6c56fb4bc5f0 240 char conv_buff[20+1];
nexpaq 0:6c56fb4bc5f0 241
nexpaq 0:6c56fb4bc5f0 242 int len = itoa_c(param, conv_buff);
nexpaq 0:6c56fb4bc5f0 243 append_raw(conv_buff, len);
nexpaq 0:6c56fb4bc5f0 244 }
nexpaq 0:6c56fb4bc5f0 245
nexpaq 0:6c56fb4bc5f0 246 int String::compare( size_type pos, size_type len, const String& str ) const {
nexpaq 0:6c56fb4bc5f0 247 int r = -1;
nexpaq 0:6c56fb4bc5f0 248 if (pos <= size_) {
nexpaq 0:6c56fb4bc5f0 249 if ( len > size_ - pos)
nexpaq 0:6c56fb4bc5f0 250 len = size_ - pos; // limit len to available length
nexpaq 0:6c56fb4bc5f0 251
nexpaq 0:6c56fb4bc5f0 252 const size_type osize = str.size();
nexpaq 0:6c56fb4bc5f0 253 const size_type len2 = std::min(len, osize);
nexpaq 0:6c56fb4bc5f0 254 r = strncmp( p + pos, str.p, len2);
nexpaq 0:6c56fb4bc5f0 255 if (r==0) // equal so far, now compare sizes
nexpaq 0:6c56fb4bc5f0 256 r = len < osize ? -1 : ( len == osize ? 0 : +1 );
nexpaq 0:6c56fb4bc5f0 257 }
nexpaq 0:6c56fb4bc5f0 258 return r;
nexpaq 0:6c56fb4bc5f0 259 }
nexpaq 0:6c56fb4bc5f0 260
nexpaq 0:6c56fb4bc5f0 261 int String::compare( size_type pos, size_type len, const char* str ) const {
nexpaq 0:6c56fb4bc5f0 262 int r = -1;
nexpaq 0:6c56fb4bc5f0 263 if (pos <= size_) {
nexpaq 0:6c56fb4bc5f0 264
nexpaq 0:6c56fb4bc5f0 265 if ( len > size_ - pos)
nexpaq 0:6c56fb4bc5f0 266 len = size_ - pos; // limit len to available length
nexpaq 0:6c56fb4bc5f0 267
nexpaq 0:6c56fb4bc5f0 268 const size_type osize = strlen(str);
nexpaq 0:6c56fb4bc5f0 269 const size_type len2 = std::min(len, osize);
nexpaq 0:6c56fb4bc5f0 270 r = strncmp( p + pos, str, len2);
nexpaq 0:6c56fb4bc5f0 271 if (r==0) // equal so far, now compare sizes
nexpaq 0:6c56fb4bc5f0 272 r = len < osize ? -1 : ( len == osize ? 0 : +1 );
nexpaq 0:6c56fb4bc5f0 273 }
nexpaq 0:6c56fb4bc5f0 274 return r;
nexpaq 0:6c56fb4bc5f0 275 }
nexpaq 0:6c56fb4bc5f0 276
nexpaq 0:6c56fb4bc5f0 277 int String::find_last_of(char c) const {
nexpaq 0:6c56fb4bc5f0 278 int r = -1;
nexpaq 0:6c56fb4bc5f0 279 char *v;
nexpaq 0:6c56fb4bc5f0 280 v = strrchr(p,c);
nexpaq 0:6c56fb4bc5f0 281 if (v != NULL) {
nexpaq 0:6c56fb4bc5f0 282 r = 0;
nexpaq 0:6c56fb4bc5f0 283 char* i = p;
nexpaq 0:6c56fb4bc5f0 284 while (v != i) {
nexpaq 0:6c56fb4bc5f0 285 i++;
nexpaq 0:6c56fb4bc5f0 286 r++;
nexpaq 0:6c56fb4bc5f0 287 }
nexpaq 0:6c56fb4bc5f0 288 }
nexpaq 0:6c56fb4bc5f0 289 return r;
nexpaq 0:6c56fb4bc5f0 290 }
nexpaq 0:6c56fb4bc5f0 291
nexpaq 0:6c56fb4bc5f0 292 void String::new_realloc( size_type n) {
nexpaq 0:6c56fb4bc5f0 293 if (n > 0 ) {
nexpaq 0:6c56fb4bc5f0 294 char* pnew = static_cast<char*>(realloc(p, n)); // could return NULL
nexpaq 0:6c56fb4bc5f0 295 if (pnew)
nexpaq 0:6c56fb4bc5f0 296 p = pnew;
nexpaq 0:6c56fb4bc5f0 297 }
nexpaq 0:6c56fb4bc5f0 298 }
nexpaq 0:6c56fb4bc5f0 299
nexpaq 0:6c56fb4bc5f0 300 void String::reserve( const size_type n) {
nexpaq 0:6c56fb4bc5f0 301 if (n >= allocated_ ) {
nexpaq 0:6c56fb4bc5f0 302 this->new_realloc(n + 1);
nexpaq 0:6c56fb4bc5f0 303 allocated_ = n + 1;
nexpaq 0:6c56fb4bc5f0 304 }
nexpaq 0:6c56fb4bc5f0 305 }
nexpaq 0:6c56fb4bc5f0 306
nexpaq 0:6c56fb4bc5f0 307 void String::resize( const size_type n) {
nexpaq 0:6c56fb4bc5f0 308 this->resize( n, 0 );
nexpaq 0:6c56fb4bc5f0 309 }
nexpaq 0:6c56fb4bc5f0 310
nexpaq 0:6c56fb4bc5f0 311 void String::resize( const size_type n, const char c) {
nexpaq 0:6c56fb4bc5f0 312 if (n < size_ ) {
nexpaq 0:6c56fb4bc5f0 313 p[n] = 0;
nexpaq 0:6c56fb4bc5f0 314 size_ = n;
nexpaq 0:6c56fb4bc5f0 315 }
nexpaq 0:6c56fb4bc5f0 316 else if (n > size_ ) {
nexpaq 0:6c56fb4bc5f0 317 this->reserve( n );
nexpaq 0:6c56fb4bc5f0 318 for (size_type i=size_; i < n; ++i )
nexpaq 0:6c56fb4bc5f0 319 p[i] = c;
nexpaq 0:6c56fb4bc5f0 320 p[n] = 0;
nexpaq 0:6c56fb4bc5f0 321 size_ = n;
nexpaq 0:6c56fb4bc5f0 322 }
nexpaq 0:6c56fb4bc5f0 323 }
nexpaq 0:6c56fb4bc5f0 324
nexpaq 0:6c56fb4bc5f0 325 void String::swap( String& s ) {
nexpaq 0:6c56fb4bc5f0 326 std::swap( allocated_, s.allocated_ );
nexpaq 0:6c56fb4bc5f0 327 std::swap( size_, s.size_ );
nexpaq 0:6c56fb4bc5f0 328 std::swap( p, s.p );
nexpaq 0:6c56fb4bc5f0 329 }
nexpaq 0:6c56fb4bc5f0 330
nexpaq 0:6c56fb4bc5f0 331
nexpaq 0:6c56fb4bc5f0 332 // Comparison
nexpaq 0:6c56fb4bc5f0 333 bool operator<( const String& s1, const String& s2 ) {
nexpaq 0:6c56fb4bc5f0 334 return strcmp( s1.c_str(), s2.c_str() ) < 0;
nexpaq 0:6c56fb4bc5f0 335 }
nexpaq 0:6c56fb4bc5f0 336
nexpaq 0:6c56fb4bc5f0 337 void reverse(char s[], uint32_t length)
nexpaq 0:6c56fb4bc5f0 338 {
nexpaq 0:6c56fb4bc5f0 339 uint32_t i, j;
nexpaq 0:6c56fb4bc5f0 340 char c;
nexpaq 0:6c56fb4bc5f0 341
nexpaq 0:6c56fb4bc5f0 342 for (i = 0, j = length-1; i<j; i++, j--) {
nexpaq 0:6c56fb4bc5f0 343 c = s[i];
nexpaq 0:6c56fb4bc5f0 344 s[i] = s[j];
nexpaq 0:6c56fb4bc5f0 345 s[j] = c;
nexpaq 0:6c56fb4bc5f0 346 }
nexpaq 0:6c56fb4bc5f0 347 }
nexpaq 0:6c56fb4bc5f0 348
nexpaq 0:6c56fb4bc5f0 349 uint32_t itoa_c (int64_t n, char s[])
nexpaq 0:6c56fb4bc5f0 350 {
nexpaq 0:6c56fb4bc5f0 351 int64_t sign;
nexpaq 0:6c56fb4bc5f0 352 uint32_t i;
nexpaq 0:6c56fb4bc5f0 353
nexpaq 0:6c56fb4bc5f0 354 if ((sign = n) < 0)
nexpaq 0:6c56fb4bc5f0 355 n = -n;
nexpaq 0:6c56fb4bc5f0 356
nexpaq 0:6c56fb4bc5f0 357 i = 0;
nexpaq 0:6c56fb4bc5f0 358
nexpaq 0:6c56fb4bc5f0 359 do {
nexpaq 0:6c56fb4bc5f0 360 s[i++] = n % 10 + '0';
nexpaq 0:6c56fb4bc5f0 361 }
nexpaq 0:6c56fb4bc5f0 362 while ((n /= 10) > 0);
nexpaq 0:6c56fb4bc5f0 363
nexpaq 0:6c56fb4bc5f0 364 if (sign < 0)
nexpaq 0:6c56fb4bc5f0 365 s[i++] = '-';
nexpaq 0:6c56fb4bc5f0 366
nexpaq 0:6c56fb4bc5f0 367 s[i] = '\0';
nexpaq 0:6c56fb4bc5f0 368
nexpaq 0:6c56fb4bc5f0 369 m2m::reverse(s, i);
nexpaq 0:6c56fb4bc5f0 370 return i;
nexpaq 0:6c56fb4bc5f0 371 }
nexpaq 0:6c56fb4bc5f0 372
nexpaq 0:6c56fb4bc5f0 373 uint8_t* String::convert_integer_to_array(int64_t value, uint8_t &size, uint8_t *array, uint32_t array_size)
nexpaq 0:6c56fb4bc5f0 374 {
nexpaq 0:6c56fb4bc5f0 375 uint8_t* buffer = NULL;
nexpaq 0:6c56fb4bc5f0 376 size = 0;
nexpaq 0:6c56fb4bc5f0 377 if (array) {
nexpaq 0:6c56fb4bc5f0 378 value = String::convert_array_to_integer(array, array_size);
nexpaq 0:6c56fb4bc5f0 379 }
nexpaq 0:6c56fb4bc5f0 380
nexpaq 0:6c56fb4bc5f0 381 if(value < 0xFF) {
nexpaq 0:6c56fb4bc5f0 382 size = 1;
nexpaq 0:6c56fb4bc5f0 383 } else if(value < 0xFFFF) {
nexpaq 0:6c56fb4bc5f0 384 size = 2;
nexpaq 0:6c56fb4bc5f0 385 } else if(value < 0xFFFFFF) {
nexpaq 0:6c56fb4bc5f0 386 size = 3;
nexpaq 0:6c56fb4bc5f0 387 } else if(value < 0xFFFFFFFF) {
nexpaq 0:6c56fb4bc5f0 388 size = 4;
nexpaq 0:6c56fb4bc5f0 389 } else if(value < 0xFFFFFFFFFF) {
nexpaq 0:6c56fb4bc5f0 390 size = 5;
nexpaq 0:6c56fb4bc5f0 391 } else if(value < 0xFFFFFFFFFFFF) {
nexpaq 0:6c56fb4bc5f0 392 size = 6;
nexpaq 0:6c56fb4bc5f0 393 } else if(value < 0xFFFFFFFFFFFFFF) {
nexpaq 0:6c56fb4bc5f0 394 size = 7;
nexpaq 0:6c56fb4bc5f0 395 } else {
nexpaq 0:6c56fb4bc5f0 396 size = 8;
nexpaq 0:6c56fb4bc5f0 397 }
nexpaq 0:6c56fb4bc5f0 398
nexpaq 0:6c56fb4bc5f0 399 buffer = (uint8_t*)malloc(size);
nexpaq 0:6c56fb4bc5f0 400 if (buffer) {
nexpaq 0:6c56fb4bc5f0 401 for (int i = 0; i < size; i++) {
nexpaq 0:6c56fb4bc5f0 402 buffer[i] = (value >> ((size - i - 1) * 8));
nexpaq 0:6c56fb4bc5f0 403 }
nexpaq 0:6c56fb4bc5f0 404 } else {
nexpaq 0:6c56fb4bc5f0 405 size = 0;
nexpaq 0:6c56fb4bc5f0 406 }
nexpaq 0:6c56fb4bc5f0 407 return buffer;
nexpaq 0:6c56fb4bc5f0 408 }
nexpaq 0:6c56fb4bc5f0 409
nexpaq 0:6c56fb4bc5f0 410 int64_t String::convert_array_to_integer(uint8_t *value, uint32_t size)
nexpaq 0:6c56fb4bc5f0 411 {
nexpaq 0:6c56fb4bc5f0 412 int64_t temp_64 = 0;
nexpaq 0:6c56fb4bc5f0 413 for (int i = size - 1; i >= 0; i--) {
nexpaq 0:6c56fb4bc5f0 414 temp_64 += (uint64_t)(*value++) << i * 8;
nexpaq 0:6c56fb4bc5f0 415 }
nexpaq 0:6c56fb4bc5f0 416 return temp_64;
nexpaq 0:6c56fb4bc5f0 417 }
nexpaq 0:6c56fb4bc5f0 418
nexpaq 0:6c56fb4bc5f0 419 } // namespace