ScummVM API documentation
as_string.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2014 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 // This class has been designed to be easy to use, but not necessarily efficiency.
32 // It doesn't use shared string memory, or reference counting. It keeps track of
33 // string length, memory size. It also makes sure that the string is null-terminated.
34 
35 #ifndef AS_STRING_H
36 #define AS_STRING_H
37 
38 #include <stdio.h>
39 #include <string.h>
40 
41 class asCString {
42 public:
43  asCString();
44  ~asCString();
45 
46 #ifdef AS_CAN_USE_CPP11
47  asCString(asCString &&);
48  asCString &operator =(asCString &&);
49 #endif // c++11
50 
51  asCString(const asCString &);
52  asCString(const char *);
53  asCString(const char *, size_t length);
54  explicit asCString(char);
55 
56  void Allocate(size_t len, bool keepData);
57  void SetLength(size_t len);
58  size_t GetLength() const;
59 
60  void Concatenate(const char *str, size_t length);
61  asCString &operator +=(const asCString &);
62  asCString &operator +=(const char *);
63  asCString &operator +=(char);
64 
65  void Assign(const char *str, size_t length);
66  asCString &operator =(const asCString &);
67  asCString &operator =(const char *);
68  asCString &operator =(char);
69 
70  asCString SubString(size_t start, size_t length = (size_t)(-1)) const;
71 
72  int FindLast(const char *str, int *count = 0) const;
73 
74  size_t Format(const char *fmt, ...);
75 
76  int Compare(const char *str) const;
77  int Compare(const asCString &str) const;
78  int Compare(const char *str, size_t length) const;
79 
80  char *AddressOf();
81  const char *AddressOf() const;
82  char &operator [](size_t index);
83  const char &operator[](size_t index) const;
84  size_t RecalculateLength();
85 
86 protected:
87  unsigned int length;
88  union {
89  char *dynamic;
90  char local[12];
91  };
92 };
93 
94 // Helper functions
95 
96 bool operator ==(const asCString &, const asCString &);
97 bool operator !=(const asCString &, const asCString &);
98 
99 bool operator ==(const asCString &, const char *);
100 bool operator !=(const asCString &, const char *);
101 
102 bool operator ==(const char *, const asCString &);
103 bool operator !=(const char *, const asCString &);
104 
105 bool operator <(const asCString &, const asCString &);
106 
107 asCString operator +(const asCString &, const char *);
108 asCString operator +(const char *, const asCString &);
109 asCString operator +(const asCString &, const asCString &);
110 
111 // a wrapper for using the pointer of asCString in asCMap
113 public:
115  asCStringPointer(const char *str, size_t len);
117 
118  const char *AddressOf() const;
119  size_t GetLength() const;
120 
121  bool operator==(const asCStringPointer &other) const;
122  bool operator<(const asCStringPointer &other) const;
123 
124 private:
125  // Either string/length or cstring is stored
126  const char *string;
127  size_t length;
128  asCString *cstring;
129 };
130 
131 #endif
Definition: as_string.h:112
Definition: as_string.h:41