OptionsUtils.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===-- OptionsUtils.cpp - clang-tidy -------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "OptionsUtils.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. namespace clang::tidy::utils::options {
  11. static const char StringsDelimiter[] = ";";
  12. std::vector<StringRef> parseStringList(StringRef Option) {
  13. Option = Option.trim().trim(StringsDelimiter);
  14. if (Option.empty())
  15. return {};
  16. std::vector<StringRef> Result;
  17. Result.reserve(Option.count(StringsDelimiter) + 1);
  18. StringRef Cur;
  19. while (std::tie(Cur, Option) = Option.split(StringsDelimiter),
  20. !Option.empty()) {
  21. Cur = Cur.trim();
  22. if (!Cur.empty())
  23. Result.push_back(Cur);
  24. }
  25. Cur = Cur.trim();
  26. if (!Cur.empty())
  27. Result.push_back(Cur);
  28. return Result;
  29. }
  30. std::vector<StringRef> parseListPair(StringRef L, StringRef R) {
  31. L = L.trim().trim(StringsDelimiter);
  32. if (L.empty())
  33. return parseStringList(R);
  34. R = R.trim().trim(StringsDelimiter);
  35. if (R.empty())
  36. return parseStringList(L);
  37. std::vector<StringRef> Result;
  38. Result.reserve(2 + L.count(StringsDelimiter) + R.count(StringsDelimiter));
  39. for (StringRef Option : {L, R}) {
  40. StringRef Cur;
  41. while (std::tie(Cur, Option) = Option.split(StringsDelimiter),
  42. !Option.empty()) {
  43. Cur = Cur.trim();
  44. if (!Cur.empty())
  45. Result.push_back(Cur);
  46. }
  47. Cur = Cur.trim();
  48. if (!Cur.empty())
  49. Result.push_back(Cur);
  50. }
  51. return Result;
  52. }
  53. std::string serializeStringList(ArrayRef<StringRef> Strings) {
  54. return llvm::join(Strings, StringsDelimiter);
  55. }
  56. } // namespace clang::tidy::utils::options