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

Last change on this file since 2260 was 2260, checked in by sam, 7 years ago

base: fix a bug in the String fixed-size constructor.

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