nexpaq / Mbed OS LED_Prox_Demo

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Prox_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Mon Sep 19 14:50:49 2016 +0000
Revision:
5:7d8e69b1d3b6
Parent:
1:55a6170b404f
Committing for event

Who changed what in which revision?

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