Empirical
ce_string.h
Go to the documentation of this file.
1 // This file is part of Empirical, https://github.com/devosoft/Empirical
2 // Copyright (C) Michigan State University, 2016.
3 // Released under the MIT Software license; see doc/LICENSE
4 //
5 //
6 // ce_string defines a limited string object for use within a constexpr class or function.
7 // Status: DESIGN
8 
9 #ifndef EMP_CE_STRING_H
10 #define EMP_CE_STRING_H
11 
12 #include <string>
13 #include <type_traits>
14 
15 #include "../tools/functions.h"
16 
17 namespace emp {
18 
19  class ce_string {
20  public:
21  using size_t = std::size_t;
22 
23  private:
24  const char * m_str;
25  size_t m_size;
26 
27  constexpr bool IsEqual(const ce_string & in, size_t first=0) const {
28  return (m_size == in.m_size) &&
29  ((m_size == first) || (m_str[first] == in.m_str[first] && IsEqual(in, first+1)));
30  }
31  constexpr bool IsLess(const ce_string & in, size_t first=0) const {
32  return (first == in.m_size) ? false :
33  ((first == m_size) || (m_str[first] < in.m_str[first] || IsLess(in, first+1)));
34  }
35  public:
36  template<size_t N>
37  constexpr ce_string(const char (&in)[N]) : m_str(in), m_size(N-1) { ; }
38  constexpr ce_string(const ce_string & in) = default;
39 
40  constexpr bool operator==(const ce_string & in) const { return IsEqual(in); }
41  constexpr bool operator!=(const ce_string & in) const { return !IsEqual(in); }
42  constexpr bool operator<(const ce_string & in) const { return IsLess(in); }
43  constexpr bool operator>(const ce_string & in) const { return in.IsLess(*this); }
44  constexpr bool operator<=(const ce_string & in) const { return !in.IsLess(*this); }
45  constexpr bool operator>=(const ce_string & in) const { return !IsLess(in); }
46 
47  constexpr size_t size() const { return m_size; }
48 
49  constexpr char operator[](const size_t pos) const {
50  // ASSERT IN RANGE?
51  return m_str[pos];
52  }
53 
54  operator std::string() const { return std::string(m_str); }
55  std::string ToString() const { return std::string(m_str); }
56  };
57 
58  // @CAO Grrr... not sure why this operator breaks everything....
59  // std::ostream & operator<<(std::ostream & out, const emp::ce_string & str) {
60  // out << (std::string) str;
61  // return out;
62  // }
63 
64 }
65 
66 #endif
constexpr bool operator>=(const ce_string &in) const
Definition: ce_string.h:45
constexpr ce_string(const char(&in)[N])
Definition: ce_string.h:37
constexpr bool operator>(const ce_string &in) const
Definition: ce_string.h:43
constexpr bool operator<(const ce_string &in) const
Definition: ce_string.h:42
constexpr char operator[](const size_t pos) const
Definition: ce_string.h:49
std::string ToString() const
Definition: ce_string.h:55
If we are in emscripten, make sure to include the header.
Definition: array.h:37
constexpr bool operator<=(const ce_string &in) const
Definition: ce_string.h:44
constexpr bool operator==(const ce_string &in) const
Definition: ce_string.h:40
constexpr size_t size() const
Definition: ce_string.h:47
constexpr bool operator!=(const ce_string &in) const
Definition: ce_string.h:41
Definition: ce_string.h:19