string_helpers.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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/StringRef.h"
  10. namespace Carbon {
  11. // Note llvm StringExtras has significant functionality which is intended to be
  12. // complementary to this.
  13. // Unescapes Carbon escape sequences in the source string. Returns std::nullopt
  14. // on bad input. `is_block_string` enables escaping unique to block string
  15. // literals, such as \<newline>.
  16. auto UnescapeStringLiteral(llvm::StringRef source, int hashtag_num = 0,
  17. bool is_block_string = false)
  18. -> std::optional<std::string>;
  19. // Parses a block string literal in `source`.
  20. auto ParseBlockStringLiteral(llvm::StringRef source, int hashtag_num = 0)
  21. -> ErrorOr<std::string>;
  22. // Returns true if the pointer is in the string ref (including equality with
  23. // `ref.end()`). This should be used instead of `<=` comparisons for
  24. // correctness.
  25. auto StringRefContainsPointer(llvm::StringRef ref, const char* ptr) -> bool;
  26. } // namespace Carbon
  27. #endif // CARBON_COMMON_STRING_HELPERS_H_