Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
m2mstring.h
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef M2M_STRING_H 00017 #define M2M_STRING_H 00018 00019 #include <stddef.h> // size_t 00020 #include <stdint.h> 00021 00022 class Test_M2MString; 00023 00024 /*! \namespace m2m 00025 * \brief Namespace defined as replace for components defined under std namespace. 00026 */ 00027 namespace m2m 00028 { 00029 00030 /*! \file m2mstring.h 00031 * \brief A simple C++ string class, used as replacement for std::string. 00032 */ 00033 class String 00034 { 00035 char* p; ///< The data. 00036 size_t allocated_; ///< The allocated memory size (including trailing NULL). 00037 size_t size_; ///< The currently used memory size (excluding trailing NULL). 00038 00039 public: 00040 typedef size_t size_type; 00041 static const size_type npos; 00042 00043 String(); 00044 ~String(); 00045 String(const String&); 00046 String(const char*); 00047 String(const char*, size_t); 00048 00049 String& operator=(const char*); 00050 String& operator=(const String&); 00051 00052 String& operator+=(const String&); 00053 String& operator+=(const char*); 00054 String& operator+=(char); 00055 void push_back(char); 00056 00057 bool operator==(const char*) const; 00058 bool operator==(const String&) const; 00059 00060 void clear(); // Set the string to empty (memory remains reserved). 00061 00062 size_type size() const { return size_; } ///< size without terminating NULL 00063 size_type length() const { return size_; } ///< as size() 00064 00065 size_type capacity() const { return allocated_-1; } 00066 00067 bool empty() const { return size_ == 0; } 00068 00069 const char* c_str() const { return p; } ///< raw data 00070 00071 /** Reserve internal string memory so that n characters can be put into the 00072 string (plus 1 for the NULL char). If there is already enough memory, 00073 nothing happens, if not, the memory is realloated to exactly this 00074 amount. 00075 */ 00076 void reserve( size_type n); 00077 00078 /** Resize string. If n is less than the current size, the string is truncated. 00079 If n is larger, the memory is reallocated to exactly this amount, and 00080 the additional characters are NULL characters. 00081 */ 00082 void resize( size_type n); 00083 00084 /** Resize string. If n is less than the current size, the string is truncated. 00085 If n is larger, the memory is reallocated to exactly this amount, and 00086 the additional characters are c characters. 00087 */ 00088 void resize( size_type n, char c); 00089 00090 /// swap contents 00091 void swap( String& ); 00092 00093 String substr(const size_type pos, size_type length) const; 00094 00095 // unchecked access: 00096 char& operator[](const size_type i) { return p[i]; } 00097 char operator[](const size_type i) const { return p[i]; } 00098 // checked access: 00099 char at(const size_type i) const; 00100 00101 /// erase len characters at position pos 00102 String& erase(size_type pos, size_type len); 00103 /// Append n characters of a string 00104 String& append(const char* str, size_type n); 00105 00106 // Append n characters of a non-zero-terminated string 00107 // (in contrast with other append(), which performs strlen() for the given string). 00108 String& append_raw(const char*, size_type); 00109 00110 // convert int to ascii and append it to end of string 00111 void append_int(int); 00112 00113 int compare( size_type pos, size_type len, const String& str ) const; 00114 int compare( size_type pos, size_type len, const char* str ) const; 00115 00116 int find_last_of(char c) const; 00117 00118 static uint8_t* convert_integer_to_array(int64_t value, uint8_t &size, const uint8_t *array = NULL, const uint32_t array_size = 0); 00119 static int64_t convert_array_to_integer(const uint8_t *value, const uint32_t size); 00120 00121 /** 00122 * Convert ASCII representation of a base 10 number to int64. This is needed 00123 * as sscanf("%lld") or atoll() are not universally available. 00124 * 00125 * @param value optionally zero terminated string containing a zero or one 00126 * sign char ('+' or '-') and a number in base 10 chars, ie. "0..9" 00127 * 00128 * @param length chars to convert, must be more or equal than the chars before zero. 00129 * This is useful for extracting values from non-zero terminated strings. 00130 * 00131 * @param conversion_result result of conversion 00132 * 00133 * @return will be set to true if at least one digit is found 00134 * and a false is returned if: 00135 * a) no digits found, ie. string is empty 00136 * b) a non-digit or non-'+' or non-'-' char is found. 00137 * c) more than one +, - chars are found 00138 * 00139 * Note: the handling of a number with digits in front and 00140 * non-digits at end (eg. "0zero") differs from sscanf(), 00141 * as sscanf will return what it got converted and a success value 00142 * even if the string ended with junk. 00143 */ 00144 static bool convert_ascii_to_int(const char *value, size_t length, int64_t &conversion_result); 00145 00146 private: 00147 // reallocate the internal memory 00148 void new_realloc( size_type n); 00149 char* strdup(const char* other); 00150 00151 friend class ::Test_M2MString; 00152 00153 }; 00154 // class 00155 00156 bool operator<(const String&, const String&); 00157 00158 void reverse(char s[], uint32_t length); 00159 00160 uint32_t itoa_c (int64_t n, char s[]); 00161 } // namespace 00162 00163 00164 #endif // M2M_STRING_H
Generated on Mon Aug 29 2022 19:53:40 by
