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
21namespace spvtools {
22namespace utils {
23
24std::string CardinalToOrdinal(size_t cardinal) {
25 const size_t mod10 = cardinal % 10;
26 const size_t mod100 = cardinal % 100;
27 std::string suffix;
28 if (mod10 == 1 && mod100 != 11)
29 suffix = "st";
30 else if (mod10 == 2 && mod100 != 12)
31 suffix = "nd";
32 else if (mod10 == 3 && mod100 != 13)
33 suffix = "rd";
34 else
35 suffix = "th";
36
37 return ToString(cardinal) + suffix;
38}
39
40std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag) {
41 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 size_t dash_ix = 0;
46 if (flag[0] == '-' && flag[1] == '-')
47 dash_ix = 2;
48 else if (flag[0] == '-')
49 dash_ix = 1;
50
51 size_t ix = flag.find('=');
52 return (ix != std::string::npos)
53 ? make_pair(flag.substr(dash_ix, ix - 2), flag.substr(ix + 1))
54 : make_pair(flag.substr(dash_ix), std::string());
55}
56
57} // namespace utils
58} // namespace spvtools
59