summaryrefslogtreecommitdiffstats
blob: c87d82b387d2f08f98f034da60e7ec0cc9e072d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#pragma once

#include <boost/algorithm/string.hpp>

#include <ostream>
#include <string>
#include <utility>
#include <vector>
#include <cassert>

#define SWIFTEN_STRING_TO_CFSTRING(a) \
	CFStringCreateWithBytes(NULL, reinterpret_cast<const UInt8*>(a.getUTF8Data()), a.getUTF8Size(), kCFStringEncodingUTF8, false)

namespace Swift {
	class ByteArray;

	class String {
			friend class ByteArray;

		public:
			String() {}
			String(const char* data) : data_(data) {}
			String(const char* data, size_t len) : data_(data, len) {}
			String(const std::string& data) : data_(data) {}

			bool isEmpty() const { return data_.empty(); }

			const char* getUTF8Data() const { return data_.c_str(); }
			const std::string& getUTF8String() const { return data_; }
			std::string& getUTF8String() { return data_; }
			size_t getUTF8Size() const { return data_.size(); }
			std::vector<unsigned int> getUnicodeCodePoints() const;

			void clear() { data_.clear(); }

			/**
			 * Returns the part before and after 'c'.
			 * If the given splitter does not occur in the string, the second
			 * component is the empty string.
			 */
			std::pair<String,String> getSplittedAtFirst(char c) const;

			std::vector<String> split(char c) const;

			size_t getLength() const;

			String getLowerCase() const {
				return boost::to_lower_copy(data_);
			}

			void removeAll(char c);

			void replaceAll(char c, const String& s);

			bool beginsWith(char c) const { 
				return data_.size() > 0 && data_[0] == c; 
			}

			bool beginsWith(const String& s) const {
				return boost::starts_with(data_, s.data_);
			}

			bool endsWith(char c) const { 
				return data_.size() > 0 && data_[data_.size()-1] == c; 
			}

			bool endsWith(const String& s) const {
				return boost::ends_with(data_, s.data_);
			}

			String getSubstring(size_t begin, size_t end) const {
				return String(data_.substr(begin, end));
			}

			size_t find(char c) const {
				assert((c & 0x80) == 0);
				return data_.find(c);
			}

			size_t npos() const {
				return data_.npos;
			}

			friend String operator+(const String& a, const String& b) {
				return String(a.data_ + b.data_);
			}

			friend String operator+(const String& a, char b) {
				return String(a.data_ + b);
			}

			String& operator+=(const String& o) {
				data_ += o.data_;
				return *this;
			}

			String& operator+=(char c) {
				data_ += c;
				return *this;
			}

			String& operator=(const String& o) {
				data_ = o.data_;
				return *this;
			}

			bool contains(const String& o) {
				return data_.find(o.data_) != std::string::npos;
			}

			char operator[](size_t i) const {
				return data_[i];
			}

			friend bool operator>(const String& a, const String& b) {
				return a.data_ > b.data_;
			}

			friend bool operator<(const String& a, const String& b) {
				return a.data_ < b.data_;
			}

			friend bool operator!=(const String& a, const String& b) {
				return a.data_ != b.data_;
			}

			friend bool operator==(const String& a, const String& b) {
				return a.data_ == b.data_;
			}

			friend std::ostream& operator<<(std::ostream& os, const String& s) {
				os << s.data_;
				return os;
			}

		private:
			std::string data_;
	};
}