NSVD Reader  0.0.1
const_string.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_CONST_STRING_HPP__
10 #define __NODAMUSHI_CONST_STRING_HPP__
11 
12 # include <cstdint>
13 # include <string>
14 # include <ostream>
15 
16 # if __cplusplus >= 201703
17 # include <string_view>
18 # include <optional>
19 # endif
20 
21 
22 # ifdef __GNUC__
23 # ifndef _ENABLE_GNU_TEMPLATE_STRING_
24 # define _ENABLE_GNU_TEMPLATE_STRING_
25 # ifdef __clang__
26 # pragma GCC diagnostic ignored "-Wgnu-string-literal-operator-template"
27 # endif
28 # endif
29 # endif
30 
31 
32 # include "nodamushi/string_type.hpp"
33 
34 namespace nodamushi{
35 
36 # if __cplusplus >= 201703
37 template<typename T>
38 using optional = std::optional<T>;
39 constexpr inline bool is_empty(const std::string_view& v){return v.empty();}
40 # endif
41 
42 constexpr inline bool is_empty(const char* v){return !v || *v == '\0';}
43 inline bool is_empty(const std::string& v){return v.empty();}
44 
45 namespace details{
46 template<char... chars>struct template_char_length;
47 template<char... chars>struct template_text;
48 }
49 
50 template<char... chars>
51 constexpr size_t get_const_string_length()
52 {
53  return details::template_char_length<chars...>::value;
54 }
55 
56 template<char... chars>
57 constexpr char at_const_string(size_t index){
58  if(sizeof...(chars) <= index){return '\0';}
59  char TEXT[] = {chars...};
60  return TEXT[index];
61 }
62 template<size_t N>
63 constexpr char at(const char (&array)[N],size_t index)
64 {
65  return index < N?array[index]:'\0';
66 }
67 template<size_t MAX_SIZE,size_t N>
68 constexpr char at0(const char (&array)[N])
69 {
70  static_assert(N <= MAX_SIZE,"N<=MAX_SIZE.");
71  return N != 0?array[0]:'\0';
72 }
73 
74 
75 template<char... chars>
77 {
78  return details::template_text<chars...>::TEXT;
79 }
80 
81 # if __cplusplus >= 201703
82 # define NODAMUSHI_LOCAL_CONST_STRING(CHARS,varname) std::string_view varname =::nodamushi::details::template_text<CHARS...>::TEXT
83 # else
84 # define NODAMUSHI_LOCAL_CONST_STRING(CHARS,varname) constexpr char varname[] = {CHARS...,'\0'}
85 # endif
86 
87 
88 
89 template<char... chars>
90 std::ostream& print_const_string(std::ostream& o)
91 {
93  o << x;
94  return o;
95 
96 }
97 }// end namespace nodamushi
98 
99 
100 
101 
102 
103 namespace nodamushi{
104 namespace details{
105 
106 template<char... chars>
107 struct template_char_length{
108  private:
109  static constexpr size_t count(){
110  char TEXT[] = {chars...,'\0'};
111  for(size_t i=0;i<sizeof...(chars);i++){
112  if(TEXT[i] == '\0')return i;
113  }
114  return sizeof...(chars);
115  }
116 
117  public:
118  static constexpr size_t value = count();
119 };
120 
121 template<char... chars>struct template_text
122 {
123 # if __cplusplus >= 201703
124  private:
125  static inline constexpr char arr[] = {chars...};
126  public:
127  static inline constexpr std::string_view TEXT = std::string_view(
129 # else
130  static constexpr char TEXT[] = {chars...,'\0'};
131 # endif
132 };
133 
134 
135 } // end namespace details
136 } // end namespace nodamushi
137 
138 #endif // __NODAMUSHI_CONST_STRING_HPP__
std::ostream & print_const_string(std::ostream &o)
constexpr char at0(const char(&array)[N])
constexpr char at_const_string(size_t index)
C++17/14 string type.
constexpr size_t get_const_string_length()
const char * const_string
Definition: string_type.hpp:43
constexpr const_string get_const_string()
#define NODAMUSHI_LOCAL_CONST_STRING(CHARS, varname)
constexpr bool is_empty(const char *v)
constexpr char at(const char(&array)[N], size_t index)
static constexpr char TEXT[]