string_helpers.h 3.0 KB

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