string_at.hpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #ifndef BOOST_METAPARSE_V1_CPP11_IMPL_STRING_AT_HPP
  2. #define BOOST_METAPARSE_V1_CPP11_IMPL_STRING_AT_HPP
  3. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2016.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/metaparse/limit_string_size.hpp>
  8. namespace boost
  9. {
  10. namespace metaparse
  11. {
  12. namespace v1
  13. {
  14. namespace impl
  15. {
  16. template <int MaxLen, int Len, class T>
  17. constexpr T string_at(const T (&s)[Len], int n)
  18. {
  19. // "MaxLen + 1" adds the \0 character of the string literal to the
  20. // limit
  21. static_assert(Len <= MaxLen + 1, "String literal is too long.");
  22. return n >= Len - 1 ? T() : s[n];
  23. }
  24. }
  25. }
  26. }
  27. }
  28. #ifdef BOOST_METAPARSE_V1_STRING_AT
  29. # error BOOST_METAPARSE_V1_STRING_AT already defined
  30. #endif
  31. #define BOOST_METAPARSE_V1_STRING_AT \
  32. ::boost::metaparse::v1::impl::string_at<BOOST_METAPARSE_LIMIT_STRING_SIZE>
  33. #endif