source: trunk/src/lol/base/string.h @ 2266

Last change on this file since 2266 was 2266, checked in by sam, 8 years ago

base: add safety asserts to the Array and String classes.

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The String class
13// ----------------
14// A very simple String class, based on Array.
15//
16
17#if !defined __LOL_BASE_STRING_H__
18#define __LOL_BASE_STRING_H__
19
20#include <lol/base/assert.h>
21#include <lol/base/array.h>
22
23#include <cstring>
24#include <cstdarg>
25
26namespace lol
27{
28
29class String : protected Array<char>
30{
31private:
32    typedef Array<char> Super;
33
34public:
35    inline String()
36      : Super()
37    {
38        Push('\0');
39    }
40
41    inline String(char const *str)
42      : Super()
43    {
44        using namespace std;
45        ASSERT(str);
46        Resize((int)strlen(str));
47        memcpy(&(*this)[0], str, Count() + 1);
48    }
49
50    inline String(char const *str, int count)
51      : Super()
52    {
53        using namespace std;
54        ASSERT(str);
55        Resize(count);
56        memcpy(&(*this)[0], str, count);
57        ((Super &)*this)[count] = '\0';
58    }
59
60    inline String(String const &s)
61      : Super((Super const &)s)
62    {
63    }
64
65    inline char &operator [](int n)
66    {
67        ASSERT(n >= 0);
68        ASSERT(n < Count() || (!n && !Count()));
69        return ((Super &)*this)[n];
70    }
71
72    inline char const &operator [](int n) const
73    {
74        ASSERT(n >= 0);
75        ASSERT(n < Count() || (!n && !Count()));
76        return ((Super const &)*this)[n];
77    }
78
79    inline char &Last()
80    {
81        ASSERT(Count() > 0);
82        return (*this)[Count() - 1];
83    }
84
85    inline char const &Last() const
86    {
87        ASSERT(Count() > 0);
88        return (*this)[Count() - 1];
89    }
90
91    inline int Count() const
92    {
93        return ((Super const &)*this).Count() - 1;
94    }
95
96    void Resize(int count)
97    {
98        ASSERT(count >= 0, "count = %d", count);
99        ((Super &)*this).Resize(count + 1);
100        ((Super &)*this).Last() = '\0';
101    }
102
103    inline String operator +(String const &s) const
104    {
105        String ret(*this);
106        return ret += s;
107    }
108
109    inline String& operator +=(String const &s)
110    {
111        /* Ignore the trailing zero we don't want */
112        --m_count;
113        (Super &)*this += (Super const &)s;
114        return *this;
115    }
116
117    inline String operator +(char c) const
118    {
119        String ret(*this);
120        return ret += c;
121    }
122
123    inline String& operator +=(char c)
124    {
125        ((Super &)*this).Last() = c;
126        ((Super &)*this).Push('\0');
127        return *this;
128    }
129
130    inline bool operator ==(String const &s) const
131    {
132        if (this->m_count != s.m_count)
133            return false;
134
135        for (int i = 0; i < this->m_count; ++i)
136            if ((*this)[i] != s[i])
137                return false;
138
139        return true;
140    }
141
142    inline bool operator !=(String const &s) const
143    {
144        return !(*this == s);
145    }
146
147#ifdef __GNUC__
148#   define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p)))
149#else
150#   define LOL_FMT_ATTR(n, p)
151#endif
152    static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2);
153#undef LOL_FMT_ATTR
154    static String Printf(char const *format, va_list ap);
155};
156
157} /* namespace lol */
158
159#endif // __LOL_BASE_STRING_H__
160
Note: See TracBrowser for help on using the repository browser.