ChaiScript / ChaiScript
1
// This file is distributed under the BSD License.
2
// See "license.txt" for details.
3
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4
// Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
5
// http://www.chaiscript.com
6

7
#ifndef CHAISCRIPT_UTILITY_STATIC_STRING_HPP_
8
#define CHAISCRIPT_UTILITY_STATIC_STRING_HPP_
9

10
namespace chaiscript
11
{
12
  namespace utility
13
  {
14

15
    struct Static_String
16
    {
17
      template<size_t N>
18 1
        constexpr Static_String(const char (&str)[N]) noexcept
19 1
        : m_size(N-1), data(&str[0])
20
        {
21
        }
22

23 1
      constexpr size_t size() const noexcept {
24 1
        return m_size;
25
      }
26

27 1
      constexpr const char *c_str() const noexcept {
28 1
        return data;
29
      }
30

31 1
      constexpr auto begin() const noexcept {
32 1
        return data;
33
      }
34

35 1
      constexpr auto end() const noexcept {
36 1
        return data + m_size;
37
      }
38

39 1
      constexpr bool operator==(const std::string_view &other) const noexcept {
40
        //return std::string_view(data, m_size) == other;
41 1
        auto b1 = begin();
42 1
        const auto e1 = end();
43 1
        auto b2 = other.begin();
44 1
        const auto e2 = other.end();
45

46 1
        if (e1 - b1 != e2 - b2) { return false; }
47

48 1
        while (b1 != e1) {
49 1
          if (*b1 != *b2) { return false; }
50 1
          ++b1; ++b2;
51
        }
52 1
        return true;
53
      }
54

55
      bool operator==(const std::string &t_str) const noexcept {
56
        return std::equal(begin(), end(), std::cbegin(t_str), std::cend(t_str));
57
      }
58

59
      const size_t m_size;
60
      const char *data = nullptr;
61
    };
62
  }
63
}
64

65
#endif

Read our documentation on viewing source code .

Loading