string_view 987 B

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