string_at.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/v1/impl/no_char.hpp>
  8. #include <boost/metaparse/limit_string_size.hpp>
  9. namespace boost
  10. {
  11. namespace metaparse
  12. {
  13. namespace v1
  14. {
  15. namespace impl
  16. {
  17. template <int MaxLen, int Len, class T>
  18. constexpr int string_at(const T (&s)[Len], int n)
  19. {
  20. // "MaxLen + 1" adds the \0 character of the string literal to the
  21. // limit
  22. static_assert(Len <= MaxLen + 1, "String literal is too long.");
  23. return n >= Len - 1 ? BOOST_NO_CHAR : s[n];
  24. }
  25. }
  26. }
  27. }
  28. }
  29. #ifdef BOOST_METAPARSE_V1_STRING_AT
  30. # error BOOST_METAPARSE_V1_STRING_AT already defined
  31. #endif
  32. #define BOOST_METAPARSE_V1_STRING_AT \
  33. ::boost::metaparse::v1::impl::string_at<BOOST_METAPARSE_LIMIT_STRING_SIZE>
  34. #endif