diff options
author | Li Jin <dragon-fly@qq.com> | 2017-07-05 14:30:02 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2017-07-05 14:30:02 +0800 |
commit | 2dc481058998344b7f97721d47987be3ac053768 (patch) | |
tree | 9afd61edfc6d3f079262a3086ebef8cb70943a31 /MoonParser/slice.h | |
parent | 2d074d650ead2142857ff9f8b192b061d1506eca (diff) | |
download | yuescript-2dc481058998344b7f97721d47987be3ac053768.tar.gz yuescript-2dc481058998344b7f97721d47987be3ac053768.tar.bz2 yuescript-2dc481058998344b7f97721d47987be3ac053768.zip |
Learn PEGTL a little.
Diffstat (limited to '')
-rw-r--r-- | MoonParser/slice.h | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/MoonParser/slice.h b/MoonParser/slice.h new file mode 100644 index 0000000..60e0ae1 --- /dev/null +++ b/MoonParser/slice.h | |||
@@ -0,0 +1,157 @@ | |||
1 | /** | ||
2 | * Copyright (C) 2017, IppClub. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
5 | * of this software and associated documentation files (the "Software"), to deal | ||
6 | * in the Software without restriction, including without limitation the rights | ||
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
8 | * copies of the Software, and to permit persons to whom the Software is | ||
9 | * furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
20 | * SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #pragma once | ||
24 | |||
25 | #include <cassert> | ||
26 | #include <cstring> | ||
27 | #include <string> | ||
28 | #include <list> | ||
29 | |||
30 | namespace silly { | ||
31 | |||
32 | namespace slice { | ||
33 | // A Slice object wraps a "const char *" or a "const std::string&" but | ||
34 | // without copying their contents. | ||
35 | class Slice { | ||
36 | private: | ||
37 | struct TrustedInitTag {}; | ||
38 | constexpr Slice(const char* s, size_t n, TrustedInitTag) : str_(s), len_(n) {} | ||
39 | |||
40 | public: | ||
41 | // implicit conversion from std::string to Slice | ||
42 | Slice(const std::string &s) : Slice(s.data(), s.size()) {} | ||
43 | |||
44 | Slice(const char *s) : Slice(s, s ? strlen(s) : 0) {} | ||
45 | |||
46 | Slice(const char *s, size_t n) : str_(s), len_(n) {} | ||
47 | |||
48 | Slice(std::pair<const char*,size_t> sp) : str_(sp.first), len_(sp.second) {} | ||
49 | |||
50 | constexpr Slice(std::nullptr_t p = nullptr) : str_(nullptr), len_(0) {} | ||
51 | |||
52 | operator std::string() const { | ||
53 | return std::string(str_, len_); | ||
54 | } | ||
55 | |||
56 | const char &operator[](size_t n) const { | ||
57 | return str_[n]; | ||
58 | } | ||
59 | |||
60 | size_t size() const { | ||
61 | return len_; | ||
62 | } | ||
63 | |||
64 | const char *rawData() const { | ||
65 | return str_; | ||
66 | } | ||
67 | |||
68 | std::string toString() const { | ||
69 | return std::string(str_, len_); | ||
70 | } | ||
71 | |||
72 | bool empty() const { | ||
73 | return len_ == 0; | ||
74 | } | ||
75 | |||
76 | // similar with std::string::compare | ||
77 | // http://en.cppreference.com/w/cpp/string/basic_string/compare | ||
78 | int compare(const Slice &rhs) const; | ||
79 | |||
80 | void skip(size_t n) { | ||
81 | assert(n <= len_); | ||
82 | str_ += n; | ||
83 | len_ -= n; | ||
84 | } | ||
85 | |||
86 | void copyTo(char *dest, bool appendEndingNull = true) const { | ||
87 | memcpy(dest, str_, len_); | ||
88 | if (appendEndingNull) { | ||
89 | dest[len_] = '\0'; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | Slice &trimSpace() { | ||
94 | assert(len_ > 0); | ||
95 | size_t start = 0, end = len_ - 1; | ||
96 | while (start < end && isspace(str_[start])) { | ||
97 | start++; | ||
98 | } | ||
99 | while (start < end && isspace(str_[end])) { | ||
100 | end--; | ||
101 | } | ||
102 | str_ += start; | ||
103 | len_ = end - start + 1; | ||
104 | return *this; | ||
105 | } | ||
106 | |||
107 | typedef const char *const_iterator; | ||
108 | |||
109 | const_iterator begin() const { | ||
110 | return str_; | ||
111 | } | ||
112 | |||
113 | const_iterator end() const { | ||
114 | return str_ + len_; | ||
115 | } | ||
116 | |||
117 | std::string getFilePath() const; | ||
118 | std::string getFileName() const; | ||
119 | std::string getFileExtension() const; | ||
120 | std::string toLower() const; | ||
121 | std::string toUpper() const; | ||
122 | std::list<Slice> split(const Slice& delimer) const; | ||
123 | |||
124 | static const std::string Empty; | ||
125 | static float stof(const Slice& str); | ||
126 | static int stoi(const Slice& str); | ||
127 | |||
128 | constexpr friend Slice operator"" _slice(const char* s, size_t n); | ||
129 | private: | ||
130 | const char *str_; | ||
131 | size_t len_; | ||
132 | }; | ||
133 | |||
134 | inline Slice trimSpace(const Slice &s) { | ||
135 | Slice tmp = s; | ||
136 | return tmp.trimSpace(); | ||
137 | } | ||
138 | |||
139 | inline bool operator==(const Slice &lhs, const Slice &rhs) { | ||
140 | return lhs.compare(rhs) == 0; | ||
141 | } | ||
142 | |||
143 | inline bool operator!=(const Slice &lhs, const Slice &rhs) { | ||
144 | return !(lhs == rhs); | ||
145 | } | ||
146 | |||
147 | inline std::string operator+(const std::string& lhs, const Slice &rhs) { | ||
148 | return lhs + rhs.toString(); | ||
149 | } | ||
150 | |||
151 | constexpr Slice operator"" _slice(const char* s, size_t n) { | ||
152 | return Slice(s, n, Slice::TrustedInitTag{}); | ||
153 | } | ||
154 | |||
155 | } // namespace slice | ||
156 | |||
157 | } // namespace silly | ||