string_helpers.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_COMMON_STRING_HELPERS_H_
  5. #define CARBON_COMMON_STRING_HELPERS_H_
  6. #include <optional>
  7. #include <string>
  8. #include "common/error.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/ADT/StringRef.h"
  12. namespace Carbon {
  13. // Note llvm StringExtras has significant functionality which is intended to be
  14. // complementary to this.
  15. // Unescapes Carbon escape sequences in the source string. Returns std::nullopt
  16. // on bad input. `is_block_string` enables escaping unique to block string
  17. // literals, such as \<newline>.
  18. auto UnescapeStringLiteral(llvm::StringRef source, int hashtag_num = 0,
  19. bool is_block_string = false)
  20. -> std::optional<std::string>;
  21. // Parses a block string literal in `source`.
  22. auto ParseBlockStringLiteral(llvm::StringRef source, int hashtag_num = 0)
  23. -> ErrorOr<std::string>;
  24. // Returns true if the pointer is in the string ref (including equality with
  25. // `ref.end()`). This should be used instead of `<=` comparisons for
  26. // correctness.
  27. auto StringRefContainsPointer(llvm::StringRef ref, const char* ptr) -> bool;
  28. // Converts `tool_path` and each of the `args` into C-strings and returns the
  29. // results. This is intended for use with APIs that expect `argv`-like command
  30. // line argument lists.
  31. //
  32. // Accepts a `cstr_arg_storage` that will provide the underlying storage for
  33. // the C-strings, and returns a small vector of the C-string pointers. The
  34. // returned small vector uses a large small size to allow most common command
  35. // lines to avoid extra allocations and growth passes.
  36. auto BuildCStrArgs(llvm::StringRef tool_path,
  37. llvm::ArrayRef<llvm::StringRef> args,
  38. llvm::OwningArrayRef<char>& cstr_arg_storage)
  39. -> llvm::SmallVector<const char*, 64>;
  40. // An overload of `BuildCStrArgs` with the same core behavior as the above, but
  41. // with an extra series of `prefix_args` that are placed between the `tool_path`
  42. // and the `args` in the resulting list.
  43. //
  44. // Unlike the tool path and the main `args`, the `prefix_args` are accepted as
  45. // an array of `std::string`s and those string object's `c_str()` method is used
  46. // to get the underlying C-strings to include in the result. This is because
  47. // callers with prefix arguments regularly need to provide dedicated storage for
  48. // these arguments anyways and we can efficiently reuse that. In contrast, the
  49. // `args` are often pulled from an existing `llvm::StringRef` that may never
  50. // exist as a valid C-string and so we need to rebuild those using the storage.
  51. auto BuildCStrArgs(llvm::StringRef tool_path,
  52. llvm::ArrayRef<std::string> prefix_args,
  53. llvm::ArrayRef<llvm::StringRef> args,
  54. llvm::OwningArrayRef<char>& cstr_arg_storage)
  55. -> llvm::SmallVector<const char*, 64>;
  56. } // namespace Carbon
  57. #endif // CARBON_COMMON_STRING_HELPERS_H_