Mistake on this page? Email us
m2mstring.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  * Licensed under the Apache License, Version 2.0 (the License); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef M2M_STRING_H
17 #define M2M_STRING_H
18 
21 #include <stddef.h> // size_t
22 #include <stdint.h>
23 
24 class Test_M2MString;
25 
29 namespace m2m
30 {
31 
32 
34  class String
35  {
36  char* p;
37  size_t allocated_;
38  size_t size_;
39 
40  public:
41  typedef size_t size_type;
42  static const size_type npos;
43 
44  String();
45  ~String();
46  String(const String&);
47  String(const char*);
48  String(const char*, size_t);
49 
50  String& operator=(const char*);
51  String& operator=(const String&);
52 
53  String& operator+=(const String&);
54  String& operator+=(const char*);
55  String& operator+=(char);
56  void push_back(char);
57 
58  bool operator==(const char*) const;
59  bool operator==(const String&) const;
60 
61  void clear(); // Set the string to empty (memory remains reserved).
62 
63  size_type size() const { return size_; }
64  size_type length() const { return size_; }
65 
66  size_type capacity() const { return allocated_-1; }
67 
68  bool empty() const { return size_ == 0; }
69 
70  const char* c_str() const { return p; }
71 
77  void reserve( size_type n);
78 
83  void resize( size_type n);
84 
89  void resize( size_type n, char c);
90 
92  void swap( String& );
93 
94  String substr(const size_type pos, size_type length) const;
95 
96  // unchecked access:
97  char& operator[](const size_type i) { return p[i]; }
98  char operator[](const size_type i) const { return p[i]; }
99  // checked access:
100  char at(const size_type i) const;
101 
103  String& erase(size_type pos, size_type len);
105  String& append(const char* str, size_type n);
106 
107  // Append n characters of a non-zero-terminated string
108  // (in contrast with other append(), which performs strlen() for the given string).
109  String& append_raw(const char*, size_type);
110 
111  // convert int to ascii and append it to end of string
112  void append_int(int);
113 
114  int compare( size_type pos, size_type len, const String& str ) const;
115  int compare( size_type pos, size_type len, const char* str ) const;
116 
117  int find_last_of(char c) const;
118  int find_first_of(char c) const;
119 
120  static uint8_t* convert_integer_to_array(int64_t value, uint8_t &size, const uint8_t *array = NULL, const uint32_t array_size = 0);
121  static int64_t convert_array_to_integer(const uint8_t *value, const uint32_t size);
122 
146  static bool convert_ascii_to_int(const char *value, size_t length, int64_t &conversion_result);
147 
160  static bool convert_ascii_to_float(const char *value, size_t size, float &conversion_result);
161 
162  private:
163  // reallocate the internal memory
164  void new_realloc( size_type n);
165  char* strdup(const char* other);
166 
167  friend class ::Test_M2MString;
168 
169  };
170  // class
171 
172  bool operator<(const String&, const String&);
173 
174  void reverse(char s[], uint32_t length);
175 
176  uint32_t itoa_c (int64_t n, char s[]);
177 } // namespace
178 
179 
180 #endif // M2M_STRING_H
String & append(const char *str, size_type n)
Append n characters of a string.
static bool convert_ascii_to_float(const char *value, size_t size, float &conversion_result)
const char * c_str() const
raw data
Definition: m2mstring.h:70
void reserve(size_type n)
void resize(size_type n)
Definition: m2mstring.h:34
size_type size() const
size without terminating NULL
Definition: m2mstring.h:63
size_type length() const
as size()
Definition: m2mstring.h:64
String & erase(size_type pos, size_type len)
erase len characters at position pos
void swap(String &)
swap contents
static bool convert_ascii_to_int(const char *value, size_t length, int64_t &conversion_result)
Namespace defined as replace for components defined under std namespace.
Definition: m2mstring.h:24