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 
19 #include <stddef.h> // size_t
20 #include <stdint.h>
21 
22 class Test_M2MString;
23 
27 namespace m2m
28 {
29 
33  class String
34  {
35  char* p;
36  size_t allocated_;
37  size_t size_;
38 
39  public:
40  typedef size_t size_type;
41  static const size_type npos;
42 
43  String();
44  ~String();
45  String(const String&);
46  String(const char*);
47  String(const char*, size_t);
48 
49  String& operator=(const char*);
50  String& operator=(const String&);
51 
52  String& operator+=(const String&);
53  String& operator+=(const char*);
54  String& operator+=(char);
55  void push_back(char);
56 
57  bool operator==(const char*) const;
58  bool operator==(const String&) const;
59 
60  void clear(); // Set the string to empty (memory remains reserved).
61 
62  size_type size() const { return size_; }
63  size_type length() const { return size_; }
64 
65  size_type capacity() const { return allocated_-1; }
66 
67  bool empty() const { return size_ == 0; }
68 
69  const char* c_str() const { return p; }
70 
76  void reserve( size_type n);
77 
82  void resize( size_type n);
83 
88  void resize( size_type n, char c);
89 
91  void swap( String& );
92 
93  String substr(const size_type pos, size_type length) const;
94 
95  // unchecked access:
96  char& operator[](const size_type i) { return p[i]; }
97  char operator[](const size_type i) const { return p[i]; }
98  // checked access:
99  char at(const size_type i) const;
100 
102  String& erase(size_type pos, size_type len);
104  String& append(const char* str, size_type n);
105 
106  // Append n characters of a non-zero-terminated string
107  // (in contrast with other append(), which performs strlen() for the given string).
108  String& append_raw(const char*, size_type);
109 
110  // convert int to ascii and append it to end of string
111  void append_int(int);
112 
113  int compare( size_type pos, size_type len, const String& str ) const;
114  int compare( size_type pos, size_type len, const char* str ) const;
115 
116  int find_last_of(char c) const;
117 
118  static uint8_t* convert_integer_to_array(int64_t value, uint8_t &size, const uint8_t *array = NULL, const uint32_t array_size = 0);
119  static int64_t convert_array_to_integer(const uint8_t *value, const uint32_t size);
120 
144  static bool convert_ascii_to_int(const char *value, size_t length, int64_t &conversion_result);
145 
146  private:
147  // reallocate the internal memory
148  void new_realloc( size_type n);
149  char* strdup(const char* other);
150 
151  friend class ::Test_M2MString;
152 
153  };
154  // class
155 
156  bool operator<(const String&, const String&);
157 
158  void reverse(char s[], uint32_t length);
159 
160  uint32_t itoa_c (int64_t n, char s[]);
161 } // namespace
162 
163 
164 #endif // M2M_STRING_H
String & append(const char *str, size_type n)
Append n characters of a string.
const char * c_str() const
raw data
Definition: m2mstring.h:69
void reserve(size_type n)
void resize(size_type n)
Definition: m2mstring.h:33
size_type size() const
size without terminating NULL
Definition: m2mstring.h:62
size_type length() const
as size()
Definition: m2mstring.h:63
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:22