/src/spirv-tools/source/util/string_utils.cpp
Line | Count | Source |
1 | | // Copyright (c) 2017 Google Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <algorithm> |
16 | | #include <cstdint> |
17 | | #include <type_traits> |
18 | | |
19 | | #include "source/util/string_utils.h" |
20 | | |
21 | | namespace spvtools { |
22 | | namespace utils { |
23 | | |
24 | 426 | std::string CardinalToOrdinal(size_t cardinal) { |
25 | 426 | const size_t mod10 = cardinal % 10; |
26 | 426 | const size_t mod100 = cardinal % 100; |
27 | 426 | std::string suffix; |
28 | 426 | if (mod10 == 1 && mod100 != 11) |
29 | 359 | suffix = "st"; |
30 | 67 | else if (mod10 == 2 && mod100 != 12) |
31 | 8 | suffix = "nd"; |
32 | 59 | else if (mod10 == 3 && mod100 != 13) |
33 | 54 | suffix = "rd"; |
34 | 5 | else |
35 | 5 | suffix = "th"; |
36 | | |
37 | 426 | return ToString(cardinal) + suffix; |
38 | 426 | } |
39 | | |
40 | 0 | std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag) { |
41 | 0 | if (flag.size() < 2) return make_pair(flag, std::string()); |
42 | | |
43 | | // Detect the last dash before the pass name. Since we have to |
44 | | // handle single dash options (-O and -Os), count up to two dashes. |
45 | 0 | size_t dash_ix = 0; |
46 | 0 | if (flag[0] == '-' && flag[1] == '-') |
47 | 0 | dash_ix = 2; |
48 | 0 | else if (flag[0] == '-') |
49 | 0 | dash_ix = 1; |
50 | |
|
51 | 0 | size_t ix = flag.find('='); |
52 | 0 | return (ix != std::string::npos) |
53 | 0 | ? make_pair(flag.substr(dash_ix, ix - 2), flag.substr(ix + 1)) |
54 | 0 | : make_pair(flag.substr(dash_ix), std::string()); |
55 | 0 | } |
56 | | |
57 | | } // namespace utils |
58 | | } // namespace spvtools |