string_utils.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include <algorithm>
  15. #include <cstdint>
  16. #include <type_traits>
  17. #include "source/util/string_utils.h"
  18. namespace spvtools {
  19. namespace utils {
  20. std::string CardinalToOrdinal(size_t cardinal) {
  21. const size_t mod10 = cardinal % 10;
  22. const size_t mod100 = cardinal % 100;
  23. std::string suffix;
  24. if (mod10 == 1 && mod100 != 11)
  25. suffix = "st";
  26. else if (mod10 == 2 && mod100 != 12)
  27. suffix = "nd";
  28. else if (mod10 == 3 && mod100 != 13)
  29. suffix = "rd";
  30. else
  31. suffix = "th";
  32. return ToString(cardinal) + suffix;
  33. }
  34. std::pair<std::string, std::string> SplitFlagArgs(const std::string& flag) {
  35. if (flag.size() < 2) return make_pair(flag, std::string());
  36. // Detect the last dash before the pass name. Since we have to
  37. // handle single dash options (-O and -Os), count up to two dashes.
  38. size_t dash_ix = 0;
  39. if (flag[0] == '-' && flag[1] == '-')
  40. dash_ix = 2;
  41. else if (flag[0] == '-')
  42. dash_ix = 1;
  43. size_t ix = flag.find('=');
  44. return (ix != std::string::npos)
  45. ? make_pair(flag.substr(dash_ix, ix - 2), flag.substr(ix + 1))
  46. : make_pair(flag.substr(dash_ix), std::string());
  47. }
  48. } // namespace utils
  49. } // namespace spvtools