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.cpp | |
| parent | 2d074d650ead2142857ff9f8b192b061d1506eca (diff) | |
| download | yuescript-2dc481058998344b7f97721d47987be3ac053768.tar.gz yuescript-2dc481058998344b7f97721d47987be3ac053768.tar.bz2 yuescript-2dc481058998344b7f97721d47987be3ac053768.zip | |
Learn PEGTL a little.
Diffstat (limited to 'MoonParser/slice.cpp')
| -rw-r--r-- | MoonParser/slice.cpp | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/MoonParser/slice.cpp b/MoonParser/slice.cpp new file mode 100644 index 0000000..7caddbc --- /dev/null +++ b/MoonParser/slice.cpp | |||
| @@ -0,0 +1,119 @@ | |||
| 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 | #include "Slice.h" | ||
| 24 | #include <cstdlib> | ||
| 25 | |||
| 26 | namespace silly { | ||
| 27 | |||
| 28 | namespace slice { | ||
| 29 | |||
| 30 | const std::string Slice::Empty; | ||
| 31 | |||
| 32 | int Slice::compare(const Slice &rhs) const { | ||
| 33 | int ret = 0; | ||
| 34 | |||
| 35 | // It's illegal to pass nullptr to memcmp. | ||
| 36 | if (str_ && rhs.str_) | ||
| 37 | ret = memcmp(str_, rhs.str_, len_); | ||
| 38 | |||
| 39 | if (ret == 0) { | ||
| 40 | // Use len_ - rhs.len_ as returned value may cause overflow | ||
| 41 | // of size_t type length. Therefore +1, -1 are returned here. | ||
| 42 | if (len_ < rhs.len_) | ||
| 43 | return -1; | ||
| 44 | else if (len_ > rhs.len_) | ||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | return ret; | ||
| 48 | } | ||
| 49 | |||
| 50 | std::string Slice::getFilePath() const { | ||
| 51 | std::string filename = toString(); | ||
| 52 | size_t pos = filename.find_last_of("/\\"); | ||
| 53 | if (pos == std::string::npos) | ||
| 54 | { | ||
| 55 | return Slice::Empty; | ||
| 56 | } | ||
| 57 | return filename.substr(0, pos) + "/"; | ||
| 58 | } | ||
| 59 | |||
| 60 | std::string Slice::getFileName() const { | ||
| 61 | std::string filename = toString(); | ||
| 62 | size_t pos = filename.find_last_of("/\\"); | ||
| 63 | if (pos == std::string::npos) { | ||
| 64 | return Slice::Empty; | ||
| 65 | } | ||
| 66 | return filename.substr(pos+1); | ||
| 67 | } | ||
| 68 | |||
| 69 | std::string Slice::toLower() const { | ||
| 70 | std::string tmp = toString(); | ||
| 71 | for (size_t i = 0; i < tmp.length(); i++) { | ||
| 72 | tmp[i] = (char)tolower(tmp[i]); | ||
| 73 | } | ||
| 74 | return tmp; | ||
| 75 | } | ||
| 76 | |||
| 77 | std::string Slice::toUpper() const { | ||
| 78 | std::string tmp = toString(); | ||
| 79 | for (size_t i = 0; i < tmp.length(); i++) { | ||
| 80 | tmp[i] = (char)toupper(tmp[i]); | ||
| 81 | } | ||
| 82 | return tmp; | ||
| 83 | } | ||
| 84 | |||
| 85 | std::string Slice::getFileExtension() const { | ||
| 86 | std::string filename = toString(); | ||
| 87 | size_t pos = filename.rfind('.'); | ||
| 88 | if (pos == std::string::npos) { | ||
| 89 | return Slice::Empty; | ||
| 90 | } | ||
| 91 | return Slice(filename.substr(pos + 1)).toLower(); | ||
| 92 | } | ||
| 93 | |||
| 94 | std::list<Slice> Slice::split(const Slice& delims) const { | ||
| 95 | std::string text = toString(); | ||
| 96 | std::string delimers = delims.toString(); | ||
| 97 | std::list<Slice> tokens; | ||
| 98 | std::size_t start = text.find_first_not_of(delimers), end = 0; | ||
| 99 | while ((end = text.find_first_of(delimers, start)) != std::string::npos) { | ||
| 100 | tokens.push_back(Slice(str_ + start, end - start)); | ||
| 101 | start = text.find_first_not_of(delimers, end); | ||
| 102 | } | ||
| 103 | if (start != std::string::npos) { | ||
| 104 | tokens.push_back(Slice(str_ + start)); | ||
| 105 | } | ||
| 106 | return tokens; | ||
| 107 | } | ||
| 108 | |||
| 109 | float Slice::stof(const Slice& str) { | ||
| 110 | return static_cast<float>(std::atof(str.toString().c_str())); | ||
| 111 | } | ||
| 112 | |||
| 113 | int Slice::stoi(const Slice& str) { | ||
| 114 | return std::atoi(str.toString().c_str()); | ||
| 115 | } | ||
| 116 | |||
| 117 | } // namespace slice | ||
| 118 | |||
| 119 | } // namespace silly | ||
