FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:35:07 2017 +0000
Revision:
0:a2cb7295a1f7
Initial commit

Who changed what in which revision?

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