mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-client/source/m2mstring.cpp@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 7 | * |
mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 15 | */ |
mbedAustin | 11:cada08fc8a70 | 16 | #include "mbed-client/m2mstring.h" |
mbedAustin | 11:cada08fc8a70 | 17 | #include <string.h> // strlen |
mbedAustin | 11:cada08fc8a70 | 18 | #include <stdlib.h> // malloc, realloc |
mbedAustin | 11:cada08fc8a70 | 19 | #include <assert.h> |
mbedAustin | 11:cada08fc8a70 | 20 | #include <algorithm> // min |
mbedAustin | 11:cada08fc8a70 | 21 | |
mbedAustin | 11:cada08fc8a70 | 22 | namespace m2m { |
mbedAustin | 11:cada08fc8a70 | 23 | |
mbedAustin | 11:cada08fc8a70 | 24 | const String::size_type String::npos = static_cast<size_t>(-1); |
mbedAustin | 11:cada08fc8a70 | 25 | |
mbedAustin | 11:cada08fc8a70 | 26 | /* |
mbedAustin | 11:cada08fc8a70 | 27 | * Like the 'new' operator, we want to guarantee that we NEVER |
mbedAustin | 11:cada08fc8a70 | 28 | * return NULL. Loop until there is free memory. |
mbedAustin | 11:cada08fc8a70 | 29 | * |
mbedAustin | 11:cada08fc8a70 | 30 | */ |
mbedAustin | 11:cada08fc8a70 | 31 | static char* malloc_never_null(const size_t b) |
mbedAustin | 11:cada08fc8a70 | 32 | { |
mbedAustin | 11:cada08fc8a70 | 33 | char *p; |
mbedAustin | 11:cada08fc8a70 | 34 | |
mbedAustin | 11:cada08fc8a70 | 35 | do { |
mbedAustin | 11:cada08fc8a70 | 36 | p = static_cast<char*>(malloc(b)); |
mbedAustin | 11:cada08fc8a70 | 37 | } while ( p == NULL ); |
mbedAustin | 11:cada08fc8a70 | 38 | |
mbedAustin | 11:cada08fc8a70 | 39 | return p; |
mbedAustin | 11:cada08fc8a70 | 40 | } |
mbedAustin | 11:cada08fc8a70 | 41 | |
mbedAustin | 11:cada08fc8a70 | 42 | /** |
mbedAustin | 11:cada08fc8a70 | 43 | * Allocates memory for the copy the string at the same time sets "this->allocated". |
mbedAustin | 11:cada08fc8a70 | 44 | * @param s |
mbedAustin | 11:cada08fc8a70 | 45 | * @return |
mbedAustin | 11:cada08fc8a70 | 46 | */ |
mbedAustin | 11:cada08fc8a70 | 47 | char* |
mbedAustin | 11:cada08fc8a70 | 48 | String::strdup_never_null(const char* s) |
mbedAustin | 11:cada08fc8a70 | 49 | { |
mbedAustin | 11:cada08fc8a70 | 50 | const size_t len = strlen(s)+1; |
mbedAustin | 11:cada08fc8a70 | 51 | char *p2 = malloc_never_null(len); |
mbedAustin | 11:cada08fc8a70 | 52 | memcpy(p2, s, len); |
mbedAustin | 11:cada08fc8a70 | 53 | allocated_=len; |
mbedAustin | 11:cada08fc8a70 | 54 | size_=len-1; |
mbedAustin | 11:cada08fc8a70 | 55 | return p2; |
mbedAustin | 11:cada08fc8a70 | 56 | } |
mbedAustin | 11:cada08fc8a70 | 57 | |
mbedAustin | 11:cada08fc8a70 | 58 | String::String() |
mbedAustin | 11:cada08fc8a70 | 59 | : p( strdup_never_null("") ) |
mbedAustin | 11:cada08fc8a70 | 60 | { |
mbedAustin | 11:cada08fc8a70 | 61 | _return_value = '\0'; |
mbedAustin | 11:cada08fc8a70 | 62 | } |
mbedAustin | 11:cada08fc8a70 | 63 | |
mbedAustin | 11:cada08fc8a70 | 64 | String::~String() |
mbedAustin | 11:cada08fc8a70 | 65 | { |
mbedAustin | 11:cada08fc8a70 | 66 | free(p); |
mbedAustin | 11:cada08fc8a70 | 67 | p = 0; |
mbedAustin | 11:cada08fc8a70 | 68 | } |
mbedAustin | 11:cada08fc8a70 | 69 | |
mbedAustin | 11:cada08fc8a70 | 70 | String::String(const String& s) |
mbedAustin | 11:cada08fc8a70 | 71 | : p(0) |
mbedAustin | 11:cada08fc8a70 | 72 | { |
mbedAustin | 11:cada08fc8a70 | 73 | if( &s != NULL ){ |
mbedAustin | 11:cada08fc8a70 | 74 | p = malloc_never_null( s.size_ + 1 ); // copy only used part |
mbedAustin | 11:cada08fc8a70 | 75 | allocated_ = s.size_ + 1; |
mbedAustin | 11:cada08fc8a70 | 76 | size_ = s.size_; |
mbedAustin | 11:cada08fc8a70 | 77 | memcpy(p, s.p, size_ + 1); |
mbedAustin | 11:cada08fc8a70 | 78 | } |
mbedAustin | 11:cada08fc8a70 | 79 | _return_value = '\0'; |
mbedAustin | 11:cada08fc8a70 | 80 | } |
mbedAustin | 11:cada08fc8a70 | 81 | |
mbedAustin | 11:cada08fc8a70 | 82 | String::String(const char* s) |
mbedAustin | 11:cada08fc8a70 | 83 | : p(strdup_never_null(s)) |
mbedAustin | 11:cada08fc8a70 | 84 | { |
mbedAustin | 11:cada08fc8a70 | 85 | _return_value = '\0'; |
mbedAustin | 11:cada08fc8a70 | 86 | } |
mbedAustin | 11:cada08fc8a70 | 87 | |
mbedAustin | 11:cada08fc8a70 | 88 | String& String::operator=(const char* s) |
mbedAustin | 11:cada08fc8a70 | 89 | { |
mbedAustin | 11:cada08fc8a70 | 90 | if ( p != s ) { |
mbedAustin | 11:cada08fc8a70 | 91 | // s could point into our own string, so we have to allocate a new string |
mbedAustin | 11:cada08fc8a70 | 92 | const size_t len = strlen(s); |
mbedAustin | 11:cada08fc8a70 | 93 | char* copy = (char*) malloc( len + 1); |
mbedAustin | 11:cada08fc8a70 | 94 | memmove(copy, s, len+1); // trailing 0 |
mbedAustin | 11:cada08fc8a70 | 95 | free( p ); |
mbedAustin | 11:cada08fc8a70 | 96 | p = copy; |
mbedAustin | 11:cada08fc8a70 | 97 | size_ = len; |
mbedAustin | 11:cada08fc8a70 | 98 | allocated_ = len+1; |
mbedAustin | 11:cada08fc8a70 | 99 | } |
mbedAustin | 11:cada08fc8a70 | 100 | |
mbedAustin | 11:cada08fc8a70 | 101 | return *this; |
mbedAustin | 11:cada08fc8a70 | 102 | } |
mbedAustin | 11:cada08fc8a70 | 103 | |
mbedAustin | 11:cada08fc8a70 | 104 | String& String::operator=(const String& s) |
mbedAustin | 11:cada08fc8a70 | 105 | { |
mbedAustin | 11:cada08fc8a70 | 106 | return operator=(s.p); |
mbedAustin | 11:cada08fc8a70 | 107 | } |
mbedAustin | 11:cada08fc8a70 | 108 | |
mbedAustin | 11:cada08fc8a70 | 109 | String& String::operator+=(const String& s) |
mbedAustin | 11:cada08fc8a70 | 110 | { |
mbedAustin | 11:cada08fc8a70 | 111 | if (s.size_ > 0){ |
mbedAustin | 11:cada08fc8a70 | 112 | this->reserve(size_ + s.size_); |
mbedAustin | 11:cada08fc8a70 | 113 | memmove(p+size_, s.p, s.size_+1); // trailing 0 |
mbedAustin | 11:cada08fc8a70 | 114 | size_ += s.size_; |
mbedAustin | 11:cada08fc8a70 | 115 | } |
mbedAustin | 11:cada08fc8a70 | 116 | return *this; |
mbedAustin | 11:cada08fc8a70 | 117 | } |
mbedAustin | 11:cada08fc8a70 | 118 | |
mbedAustin | 11:cada08fc8a70 | 119 | // since p and s may overlap, we have to copy our own string first |
mbedAustin | 11:cada08fc8a70 | 120 | String& String::operator+=(const char* s) |
mbedAustin | 11:cada08fc8a70 | 121 | { |
mbedAustin | 11:cada08fc8a70 | 122 | const size_type lens = strlen(s); |
mbedAustin | 11:cada08fc8a70 | 123 | if (lens > 0){ |
mbedAustin | 11:cada08fc8a70 | 124 | if (size_ + lens + 1 <= allocated_) { |
mbedAustin | 11:cada08fc8a70 | 125 | memmove(p+size_, s, lens+1); // trailing 0 |
mbedAustin | 11:cada08fc8a70 | 126 | size_ += lens; |
mbedAustin | 11:cada08fc8a70 | 127 | } else { |
mbedAustin | 11:cada08fc8a70 | 128 | String s2( *this ); // copy own data |
mbedAustin | 11:cada08fc8a70 | 129 | s2.reserve(size_ + lens); |
mbedAustin | 11:cada08fc8a70 | 130 | memmove(s2.p+size_, s, lens+1); // trailing 0 |
mbedAustin | 11:cada08fc8a70 | 131 | s2.size_ = size_ + lens; |
mbedAustin | 11:cada08fc8a70 | 132 | this->swap( s2 ); |
mbedAustin | 11:cada08fc8a70 | 133 | } |
mbedAustin | 11:cada08fc8a70 | 134 | } |
mbedAustin | 11:cada08fc8a70 | 135 | return *this; |
mbedAustin | 11:cada08fc8a70 | 136 | } |
mbedAustin | 11:cada08fc8a70 | 137 | |
mbedAustin | 11:cada08fc8a70 | 138 | String& String::operator+=(const char c) |
mbedAustin | 11:cada08fc8a70 | 139 | { |
mbedAustin | 11:cada08fc8a70 | 140 | push_back(c); |
mbedAustin | 11:cada08fc8a70 | 141 | return *this; |
mbedAustin | 11:cada08fc8a70 | 142 | } |
mbedAustin | 11:cada08fc8a70 | 143 | |
mbedAustin | 11:cada08fc8a70 | 144 | |
mbedAustin | 11:cada08fc8a70 | 145 | void String::push_back(const char c) { |
mbedAustin | 11:cada08fc8a70 | 146 | |
mbedAustin | 11:cada08fc8a70 | 147 | if (size_ == allocated_ - 1) { |
mbedAustin | 11:cada08fc8a70 | 148 | size_t more = (allocated_* 3) / 2; // factor 1.5 |
mbedAustin | 11:cada08fc8a70 | 149 | if ( more < 4 ) more = 4; |
mbedAustin | 11:cada08fc8a70 | 150 | reserve( size_ + more ); |
mbedAustin | 11:cada08fc8a70 | 151 | } |
mbedAustin | 11:cada08fc8a70 | 152 | |
mbedAustin | 11:cada08fc8a70 | 153 | p[size_] = c; |
mbedAustin | 11:cada08fc8a70 | 154 | size_++; |
mbedAustin | 11:cada08fc8a70 | 155 | p[size_] = 0; |
mbedAustin | 11:cada08fc8a70 | 156 | } |
mbedAustin | 11:cada08fc8a70 | 157 | |
mbedAustin | 11:cada08fc8a70 | 158 | bool String::operator==(const char* s) const |
mbedAustin | 11:cada08fc8a70 | 159 | { |
mbedAustin | 11:cada08fc8a70 | 160 | if( s == NULL ){ |
mbedAustin | 11:cada08fc8a70 | 161 | if( p == NULL ){ |
mbedAustin | 11:cada08fc8a70 | 162 | return true; |
mbedAustin | 11:cada08fc8a70 | 163 | } |
mbedAustin | 11:cada08fc8a70 | 164 | return false; |
mbedAustin | 11:cada08fc8a70 | 165 | } |
mbedAustin | 11:cada08fc8a70 | 166 | bool ret = strcmp(p, s); |
mbedAustin | 11:cada08fc8a70 | 167 | return !ret; |
mbedAustin | 11:cada08fc8a70 | 168 | } |
mbedAustin | 11:cada08fc8a70 | 169 | |
mbedAustin | 11:cada08fc8a70 | 170 | bool String::operator==(const String& s) const |
mbedAustin | 11:cada08fc8a70 | 171 | { |
mbedAustin | 11:cada08fc8a70 | 172 | bool ret = strcmp(p, s.p); |
mbedAustin | 11:cada08fc8a70 | 173 | return !ret; |
mbedAustin | 11:cada08fc8a70 | 174 | } |
mbedAustin | 11:cada08fc8a70 | 175 | |
mbedAustin | 11:cada08fc8a70 | 176 | void String::clear() |
mbedAustin | 11:cada08fc8a70 | 177 | { |
mbedAustin | 11:cada08fc8a70 | 178 | size_ = 0; |
mbedAustin | 11:cada08fc8a70 | 179 | p[0] = 0; |
mbedAustin | 11:cada08fc8a70 | 180 | } |
mbedAustin | 11:cada08fc8a70 | 181 | |
mbedAustin | 11:cada08fc8a70 | 182 | // String operator+(const String& lhs, const String& rhs) |
mbedAustin | 11:cada08fc8a70 | 183 | // { |
mbedAustin | 11:cada08fc8a70 | 184 | // return String(lhs) += rhs; |
mbedAustin | 11:cada08fc8a70 | 185 | // } |
mbedAustin | 11:cada08fc8a70 | 186 | |
mbedAustin | 11:cada08fc8a70 | 187 | String String::substr(const size_type pos, size_type length) const |
mbedAustin | 11:cada08fc8a70 | 188 | { |
mbedAustin | 11:cada08fc8a70 | 189 | String s; |
mbedAustin | 11:cada08fc8a70 | 190 | const size_type len = size_; |
mbedAustin | 11:cada08fc8a70 | 191 | |
mbedAustin | 11:cada08fc8a70 | 192 | if ( pos <= len ) { |
mbedAustin | 11:cada08fc8a70 | 193 | |
mbedAustin | 11:cada08fc8a70 | 194 | size_type remain = len - pos; |
mbedAustin | 11:cada08fc8a70 | 195 | |
mbedAustin | 11:cada08fc8a70 | 196 | if ( length > remain ) |
mbedAustin | 11:cada08fc8a70 | 197 | length = remain; |
mbedAustin | 11:cada08fc8a70 | 198 | |
mbedAustin | 11:cada08fc8a70 | 199 | s.reserve( length ); |
mbedAustin | 11:cada08fc8a70 | 200 | |
mbedAustin | 11:cada08fc8a70 | 201 | memcpy(s.p, p + pos, length); |
mbedAustin | 11:cada08fc8a70 | 202 | s.p[length] = '\0'; |
mbedAustin | 11:cada08fc8a70 | 203 | s.size_ = length; |
mbedAustin | 11:cada08fc8a70 | 204 | } |
mbedAustin | 11:cada08fc8a70 | 205 | return s; |
mbedAustin | 11:cada08fc8a70 | 206 | } |
mbedAustin | 11:cada08fc8a70 | 207 | |
mbedAustin | 11:cada08fc8a70 | 208 | |
mbedAustin | 11:cada08fc8a70 | 209 | // checked access, accessing the NUL at end is allowed |
mbedAustin | 11:cada08fc8a70 | 210 | char& String::at(const size_type i) |
mbedAustin | 11:cada08fc8a70 | 211 | { |
mbedAustin | 11:cada08fc8a70 | 212 | if ( i <= strlen(p) ) { |
mbedAustin | 11:cada08fc8a70 | 213 | _return_value = p[i]; |
mbedAustin | 11:cada08fc8a70 | 214 | } else { |
mbedAustin | 11:cada08fc8a70 | 215 | _return_value = '\0'; |
mbedAustin | 11:cada08fc8a70 | 216 | } |
mbedAustin | 11:cada08fc8a70 | 217 | return _return_value; |
mbedAustin | 11:cada08fc8a70 | 218 | } |
mbedAustin | 11:cada08fc8a70 | 219 | |
mbedAustin | 11:cada08fc8a70 | 220 | char String::at(const size_type i) const |
mbedAustin | 11:cada08fc8a70 | 221 | { |
mbedAustin | 11:cada08fc8a70 | 222 | if ( i <= strlen(p) ) { |
mbedAustin | 11:cada08fc8a70 | 223 | return p[i]; |
mbedAustin | 11:cada08fc8a70 | 224 | } else { |
mbedAustin | 11:cada08fc8a70 | 225 | return '\0'; |
mbedAustin | 11:cada08fc8a70 | 226 | } |
mbedAustin | 11:cada08fc8a70 | 227 | } |
mbedAustin | 11:cada08fc8a70 | 228 | |
mbedAustin | 11:cada08fc8a70 | 229 | String& String::erase(size_type pos, size_type len) |
mbedAustin | 11:cada08fc8a70 | 230 | { |
mbedAustin | 11:cada08fc8a70 | 231 | if (len > 0) { |
mbedAustin | 11:cada08fc8a70 | 232 | |
mbedAustin | 11:cada08fc8a70 | 233 | if ( pos < size_ ) { // user must not remove trailing 0 |
mbedAustin | 11:cada08fc8a70 | 234 | |
mbedAustin | 11:cada08fc8a70 | 235 | size_type s2 = size_; |
mbedAustin | 11:cada08fc8a70 | 236 | size_type remain = s2 - pos - len; |
mbedAustin | 11:cada08fc8a70 | 237 | |
mbedAustin | 11:cada08fc8a70 | 238 | if (remain > 0) { |
mbedAustin | 11:cada08fc8a70 | 239 | // erase by overwriting |
mbedAustin | 11:cada08fc8a70 | 240 | memmove(p + pos, p + pos + len, remain); |
mbedAustin | 11:cada08fc8a70 | 241 | } |
mbedAustin | 11:cada08fc8a70 | 242 | |
mbedAustin | 11:cada08fc8a70 | 243 | //if ( remain < 0 ) remain = 0; |
mbedAustin | 11:cada08fc8a70 | 244 | |
mbedAustin | 11:cada08fc8a70 | 245 | // remove unused space |
mbedAustin | 11:cada08fc8a70 | 246 | this->resize( pos+remain ); |
mbedAustin | 11:cada08fc8a70 | 247 | |
mbedAustin | 11:cada08fc8a70 | 248 | } |
mbedAustin | 11:cada08fc8a70 | 249 | } |
mbedAustin | 11:cada08fc8a70 | 250 | return *this; |
mbedAustin | 11:cada08fc8a70 | 251 | } |
mbedAustin | 11:cada08fc8a70 | 252 | |
mbedAustin | 11:cada08fc8a70 | 253 | String& String::append( const char* str, size_type n) { |
mbedAustin | 11:cada08fc8a70 | 254 | if (str && n > 0) { |
mbedAustin | 11:cada08fc8a70 | 255 | size_t lens = strlen(str); |
mbedAustin | 11:cada08fc8a70 | 256 | if (n > lens) |
mbedAustin | 11:cada08fc8a70 | 257 | n = lens; |
mbedAustin | 11:cada08fc8a70 | 258 | size_t newlen = size_ + n; |
mbedAustin | 11:cada08fc8a70 | 259 | this->reserve( newlen ); |
mbedAustin | 11:cada08fc8a70 | 260 | memmove(p+size_, str, n); // p and s.p MAY overlap |
mbedAustin | 11:cada08fc8a70 | 261 | p[newlen] = 0; // add NUL termination |
mbedAustin | 11:cada08fc8a70 | 262 | size_ = newlen; |
mbedAustin | 11:cada08fc8a70 | 263 | } |
mbedAustin | 11:cada08fc8a70 | 264 | return *this; |
mbedAustin | 11:cada08fc8a70 | 265 | } |
mbedAustin | 11:cada08fc8a70 | 266 | |
mbedAustin | 11:cada08fc8a70 | 267 | int |
mbedAustin | 11:cada08fc8a70 | 268 | String::compare( size_type pos, size_type len, const String& str ) const { |
mbedAustin | 11:cada08fc8a70 | 269 | int r = -1; |
mbedAustin | 11:cada08fc8a70 | 270 | if (pos <= size_) { |
mbedAustin | 11:cada08fc8a70 | 271 | if ( len > size_ - pos) |
mbedAustin | 11:cada08fc8a70 | 272 | len = size_ - pos; // limit len to available length |
mbedAustin | 11:cada08fc8a70 | 273 | |
mbedAustin | 11:cada08fc8a70 | 274 | const size_type osize = str.size(); |
mbedAustin | 11:cada08fc8a70 | 275 | const size_type len2 = std::min(len, osize); |
mbedAustin | 11:cada08fc8a70 | 276 | r = strncmp( p + pos, str.p, len2); |
mbedAustin | 11:cada08fc8a70 | 277 | if (r==0) // equal so far, now compare sizes |
mbedAustin | 11:cada08fc8a70 | 278 | r = len < osize ? -1 : ( len == osize ? 0 : +1 ); |
mbedAustin | 11:cada08fc8a70 | 279 | } |
mbedAustin | 11:cada08fc8a70 | 280 | return r; |
mbedAustin | 11:cada08fc8a70 | 281 | } |
mbedAustin | 11:cada08fc8a70 | 282 | |
mbedAustin | 11:cada08fc8a70 | 283 | int |
mbedAustin | 11:cada08fc8a70 | 284 | String::compare( size_type pos, size_type len, const char* str ) const { |
mbedAustin | 11:cada08fc8a70 | 285 | int r = -1; |
mbedAustin | 11:cada08fc8a70 | 286 | if (pos <= size_) { |
mbedAustin | 11:cada08fc8a70 | 287 | |
mbedAustin | 11:cada08fc8a70 | 288 | if ( len > size_ - pos) |
mbedAustin | 11:cada08fc8a70 | 289 | len = size_ - pos; // limit len to available length |
mbedAustin | 11:cada08fc8a70 | 290 | |
mbedAustin | 11:cada08fc8a70 | 291 | const size_type osize = strlen(str); |
mbedAustin | 11:cada08fc8a70 | 292 | const size_type len2 = std::min(len, osize); |
mbedAustin | 11:cada08fc8a70 | 293 | r = strncmp( p + pos, str, len2); |
mbedAustin | 11:cada08fc8a70 | 294 | if (r==0) // equal so far, now compare sizes |
mbedAustin | 11:cada08fc8a70 | 295 | r = len < osize ? -1 : ( len == osize ? 0 : +1 ); |
mbedAustin | 11:cada08fc8a70 | 296 | } |
mbedAustin | 11:cada08fc8a70 | 297 | return r; |
mbedAustin | 11:cada08fc8a70 | 298 | } |
mbedAustin | 11:cada08fc8a70 | 299 | |
mbedAustin | 11:cada08fc8a70 | 300 | int |
mbedAustin | 11:cada08fc8a70 | 301 | String::find_last_of(char c) const { |
mbedAustin | 11:cada08fc8a70 | 302 | int r = -1; |
mbedAustin | 11:cada08fc8a70 | 303 | char *v; |
mbedAustin | 11:cada08fc8a70 | 304 | v = strrchr(p,c); |
mbedAustin | 11:cada08fc8a70 | 305 | if (v != NULL){ |
mbedAustin | 11:cada08fc8a70 | 306 | r = 0; |
mbedAustin | 11:cada08fc8a70 | 307 | char* i = p; |
mbedAustin | 11:cada08fc8a70 | 308 | while (v != i){ |
mbedAustin | 11:cada08fc8a70 | 309 | i++; |
mbedAustin | 11:cada08fc8a70 | 310 | r++; |
mbedAustin | 11:cada08fc8a70 | 311 | } |
mbedAustin | 11:cada08fc8a70 | 312 | } |
mbedAustin | 11:cada08fc8a70 | 313 | return r; |
mbedAustin | 11:cada08fc8a70 | 314 | } |
mbedAustin | 11:cada08fc8a70 | 315 | |
mbedAustin | 11:cada08fc8a70 | 316 | void |
mbedAustin | 11:cada08fc8a70 | 317 | String::new_realloc( size_type n) { |
mbedAustin | 11:cada08fc8a70 | 318 | if (n > 0 ) { |
mbedAustin | 11:cada08fc8a70 | 319 | char* pnew = static_cast<char*>(realloc(p, n)); // could return NULL |
mbedAustin | 11:cada08fc8a70 | 320 | if (pnew) |
mbedAustin | 11:cada08fc8a70 | 321 | p = pnew; |
mbedAustin | 11:cada08fc8a70 | 322 | } |
mbedAustin | 11:cada08fc8a70 | 323 | } |
mbedAustin | 11:cada08fc8a70 | 324 | |
mbedAustin | 11:cada08fc8a70 | 325 | |
mbedAustin | 11:cada08fc8a70 | 326 | void |
mbedAustin | 11:cada08fc8a70 | 327 | String::reserve( const size_type n) { |
mbedAustin | 11:cada08fc8a70 | 328 | if (n >= allocated_ ) { |
mbedAustin | 11:cada08fc8a70 | 329 | this->new_realloc(n + 1); |
mbedAustin | 11:cada08fc8a70 | 330 | allocated_ = n + 1; |
mbedAustin | 11:cada08fc8a70 | 331 | } |
mbedAustin | 11:cada08fc8a70 | 332 | } |
mbedAustin | 11:cada08fc8a70 | 333 | |
mbedAustin | 11:cada08fc8a70 | 334 | void |
mbedAustin | 11:cada08fc8a70 | 335 | String::resize( const size_type n) { |
mbedAustin | 11:cada08fc8a70 | 336 | this->resize( n, 0 ); |
mbedAustin | 11:cada08fc8a70 | 337 | } |
mbedAustin | 11:cada08fc8a70 | 338 | |
mbedAustin | 11:cada08fc8a70 | 339 | void |
mbedAustin | 11:cada08fc8a70 | 340 | String::resize( const size_type n, const char c) { |
mbedAustin | 11:cada08fc8a70 | 341 | if (n < size_ ) { |
mbedAustin | 11:cada08fc8a70 | 342 | p[n] = 0; |
mbedAustin | 11:cada08fc8a70 | 343 | size_ = n; |
mbedAustin | 11:cada08fc8a70 | 344 | } |
mbedAustin | 11:cada08fc8a70 | 345 | else if (n > size_ ) { |
mbedAustin | 11:cada08fc8a70 | 346 | this->reserve( n ); |
mbedAustin | 11:cada08fc8a70 | 347 | for (size_type i=size_; i < n; ++i ) |
mbedAustin | 11:cada08fc8a70 | 348 | p[i] = c; |
mbedAustin | 11:cada08fc8a70 | 349 | p[n] = 0; |
mbedAustin | 11:cada08fc8a70 | 350 | size_ = n; |
mbedAustin | 11:cada08fc8a70 | 351 | } |
mbedAustin | 11:cada08fc8a70 | 352 | } |
mbedAustin | 11:cada08fc8a70 | 353 | |
mbedAustin | 11:cada08fc8a70 | 354 | void String::swap( String& s ) { |
mbedAustin | 11:cada08fc8a70 | 355 | std::swap( allocated_, s.allocated_ ); |
mbedAustin | 11:cada08fc8a70 | 356 | std::swap( size_, s.size_ ); |
mbedAustin | 11:cada08fc8a70 | 357 | std::swap( p, s.p ); |
mbedAustin | 11:cada08fc8a70 | 358 | } |
mbedAustin | 11:cada08fc8a70 | 359 | |
mbedAustin | 11:cada08fc8a70 | 360 | |
mbedAustin | 11:cada08fc8a70 | 361 | // Comparison |
mbedAustin | 11:cada08fc8a70 | 362 | bool operator<( const String& s1, const String& s2 ) { |
mbedAustin | 11:cada08fc8a70 | 363 | return strcmp( s1.c_str(), s2.c_str() ) < 0; |
mbedAustin | 11:cada08fc8a70 | 364 | } |
mbedAustin | 11:cada08fc8a70 | 365 | |
mbedAustin | 11:cada08fc8a70 | 366 | } // namespace |