string_view 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // -*-c++-*-
  2. // vim: set ft=cpp:
  3. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  4. file Copyright.txt or https://cmake.org/licensing for details. */
  5. #ifndef cmext_string_view
  6. #define cmext_string_view
  7. #include <cstddef>
  8. #include <cm/string_view>
  9. namespace cm {
  10. /** A string_view that only binds to static storage.
  11. *
  12. * This is used together with the `""_s` user-defined literal operator
  13. * to construct a type-safe abstraction of a string_view that only views
  14. * statically allocated strings. These strings are const and available
  15. * for the entire lifetime of the program.
  16. */
  17. class static_string_view : public string_view
  18. {
  19. static_string_view(string_view v)
  20. : string_view(v)
  21. {
  22. }
  23. friend static_string_view operator"" _s(const char* data, size_t size);
  24. };
  25. /** Create a static_string_view using `""_s` literal syntax. */
  26. inline static_string_view operator"" _s(const char* data, size_t size)
  27. {
  28. return string_view(data, size);
  29. }
  30. } // namespace cm
  31. using cm::operator"" _s;
  32. #endif