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

Last change on this file since 2235 was 2235, checked in by sam, 10 years ago

base: implement String::Resize().

  • Property svn:keywords set to Id
File size: 2.5 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_CORE_STRING_H__
18#define __LOL_CORE_STRING_H__
19
20#include <lol/base/array.h>
21
22namespace lol
23{
24
25class String : protected Array<char>
26{
27private:
28    typedef Array<char> Super;
29
30public:
31    inline String()
32      : Super()
33    {
34        Push('\0');
35    }
36
37    inline String(char const *str)
38      : Super()
39    {
40        do
41        {
42            Push(*str);
43        }
44        while (*str++);
45    }
46
47    inline String(String const &s)
48      : Super((Super const &)s)
49    {
50    }
51
52    inline char &operator [](int n)
53    {
54        return ((Super &)*this)[n];
55    }
56
57    inline char const &operator [](int n) const
58    {
59        return ((Super const &)*this)[n];
60    }
61
62    inline int Count() const
63    {
64        return ((Super const &)*this).Count() - 1;
65    }
66
67    void Resize(int count)
68    {
69        ((Super &)*this).Resize(count + 1);
70        ((Super &)*this).Last() = '\0';
71    }
72
73    inline String operator +(String const &s) const
74    {
75        String ret(*this);
76        return ret += s;
77    }
78
79    inline String& operator +=(String const &s)
80    {
81        /* Ignore the trailing zero we don't want */
82        --m_count;
83        (Super &)*this += (Super const &)s;
84        return *this;
85    }
86
87    inline String operator +(char c) const
88    {
89        String ret(*this);
90        return ret += c;
91    }
92
93    inline String& operator +=(char c)
94    {
95        ((Super &)*this).Last() = c;
96        ((Super &)*this).Push('\0');
97        return *this;
98    }
99
100    inline bool operator ==(String const &s) const
101    {
102        if (this->m_count != s.m_count)
103            return false;
104
105        for (int i = 0; i < this->m_count; ++i)
106            if ((*this)[i] != s[i])
107                return false;
108
109        return true;
110    }
111
112    inline bool operator !=(String const &s) const
113    {
114        return !(*this == s);
115    }
116
117#ifdef __GNUC__
118#   define LOL_FMT_ATTR(n, p) __attribute__((format(printf, n, p)))
119#else
120#   define LOL_FMT_ATTR(n, p)
121#endif
122    static String Printf(char const *format, ...) LOL_FMT_ATTR(1, 2);
123#undef LOL_FMT_ATTR
124};
125
126} /* namespace lol */
127
128#endif // __LOL_CORE_STRING_H__
129
Note: See TracBrowser for help on using the repository browser.