├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── example ├── CMakeLists.txt └── main.cpp └── include ├── const_expr_string.hpp ├── const_expr_string_cpp11.hpp ├── const_expr_string_cpp14.hpp ├── const_expr_string_helper_defs.hpp └── const_expr_string_universal.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | # Editor files 31 | *.swp 32 | 33 | # CMake files 34 | build/ 35 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.7) 2 | 3 | project(const_expr_string) 4 | 5 | if(NOT ${CMAKE_VERSION} LESS 3.2) 6 | set(CMAKE_CXX_STANDARD 11) 7 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 8 | else() 9 | message(STATUS "Checking compiler flags for C++11 support.") 10 | # Set C++11 support flags for various compilers 11 | include(CheckCXXCompilerFlag) 12 | check_cxx_compiler_flag("-std=c++11" COMPILER_SUPPORTS_CXX11) 13 | check_cxx_compiler_flag("-std=c++0x" COMPILER_SUPPORTS_CXX0X) 14 | if(COMPILER_SUPPORTS_CXX11) 15 | message(STATUS "C++11 is supported.") 16 | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 17 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++") 18 | else() 19 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 20 | endif() 21 | elseif(COMPILER_SUPPORTS_CXX0X) 22 | message(STATUS "C++0x is supported.") 23 | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 24 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++") 25 | else() 26 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") 27 | endif() 28 | else() 29 | message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") 30 | endif() 31 | endif() 32 | 33 | set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) 34 | set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) 35 | 36 | set(CES_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "Location of const_expr_string headers") 37 | 38 | add_subdirectory(example) 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # const_expr_string 2 | An entirely const_expr string class in C++. Similar to string_view, most useful for compile-time constants 3 | 4 | This header-only class is meant to be an entirely compile-time string wrapper, specifically as a replacement 5 | for static-const char and #define X string constants. 6 | 7 | It emulates string_view as close as possible. 8 | 9 | Compatible with C++14 AND C++11 (though separate implementations for each for a few functions). Tested on G++4.9 and 5.2, and clang++ 3.5 and 3.6.1. 10 | 11 | To compile the main test, just do one of the following: 12 | 13 | ```shell 14 | g++ -std=c++14 -O3 -Wextra -Wall -Werror main.cpp 15 | ``` 16 | ```shell 17 | g++ -std=c++11 -O3 -Wextra -Wall -Werror main.cpp 18 | ``` 19 | ```shell 20 | clang++ -std=c++14 -O3 -Wextra -Wall -Werror main.cpp 21 | ``` 22 | ```shell 23 | clang++ -std=c++11 -O3 -Wextra -Wall -Werror main.cpp 24 | ``` 25 | 26 | Or, if you have CMake: 27 | 28 | ```shell 29 | mkdir build 30 | cd build 31 | cmake .. 32 | make 33 | ``` 34 | 35 | This will produce a binary called `main` in `build/bin/`. 36 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include_directories(${CES_INCLUDE_DIRS}) 2 | add_executable(main main.cpp) 3 | -------------------------------------------------------------------------------- /example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "include/const_expr_string.hpp" 6 | 7 | struct test_ctors 8 | { 9 | static constexpr erichkeane::const_expr_string ctor1 = "asdfasdfasdf"; 10 | static constexpr erichkeane::const_expr_string ctor2 = ctor1; 11 | constexpr bool operator()() const 12 | { 13 | return true; 14 | } 15 | }; 16 | 17 | struct test_char_conversion 18 | { 19 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 20 | static constexpr erichkeane::const_expr_string ctor1 = root; 21 | static constexpr erichkeane::const_expr_string ctor2 = ctor1; 22 | 23 | static constexpr const char* temp1 = ctor1; 24 | static constexpr const char* temp2 = ctor2; 25 | 26 | constexpr bool operator()() const 27 | { 28 | static_assert(&temp1[0] == &root[0], "test_output failed"); 29 | static_assert(&temp2[0] == &root[0], "test_output failed"); 30 | 31 | return true; 32 | } 33 | }; 34 | 35 | struct test_sizes 36 | { 37 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 38 | static constexpr erichkeane::const_expr_string ctor1 = root; 39 | 40 | constexpr bool operator()() const 41 | { 42 | static_assert(ctor1.size() == sizeof("asdfqefaxdvaqwerasdf") - 1, "test_sizes failed"); 43 | static_assert(ctor1.length() == sizeof("asdfqefaxdvaqwerasdf") - 1, "test_sizes failed"); 44 | 45 | return true; 46 | } 47 | }; 48 | 49 | struct test_iterators 50 | { 51 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 52 | static constexpr erichkeane::const_expr_string ctor1 = root; 53 | using It = typename erichkeane::const_expr_string::iterator; 54 | 55 | constexpr bool helper(size_t i, It iterator) const 56 | { 57 | return iterator < ctor1.end() ? 58 | ( 59 | *iterator != root[i] ? false : helper(i + 1, iterator + 1) 60 | ) 61 | : true; 62 | } 63 | constexpr bool operator()() const 64 | { 65 | static_assert(*(ctor1.begin()) == root[0], "Iterator Begin Failed"); 66 | return ctor1.begin() != ctor1.end() && helper(0, ctor1.begin()); 67 | } 68 | }; 69 | 70 | struct test_const_iterators 71 | { 72 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 73 | static constexpr erichkeane::const_expr_string ctor1 = root; 74 | using It = typename erichkeane::const_expr_string::const_iterator; 75 | 76 | constexpr bool helper(size_t i, It iterator) const 77 | { 78 | return iterator < ctor1.cend() ? 79 | ( 80 | *iterator != root[i] ? false : helper(i + 1, iterator + 1) 81 | ) 82 | : true; 83 | } 84 | constexpr bool operator()() const 85 | { 86 | static_assert(*(ctor1.cbegin()) == root[0], "Iterator Begin Failed"); 87 | return ctor1.cbegin() != ctor1.cend() && helper(0, ctor1.cbegin()); 88 | } 89 | }; 90 | 91 | struct test_index_op 92 | { 93 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 94 | static constexpr erichkeane::const_expr_string ctor1 = root; 95 | 96 | constexpr bool helper(erichkeane::const_expr_string::size_type i) const 97 | { 98 | return i < ctor1.size() ? 99 | ( 100 | ctor1[i] != root[i] ? false : helper(i+1) 101 | ) 102 | :true; 103 | } 104 | constexpr bool operator()() const 105 | { 106 | return helper(0); 107 | } 108 | }; 109 | 110 | struct test_at 111 | { 112 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 113 | static constexpr erichkeane::const_expr_string ctor1 = root; 114 | 115 | constexpr bool helper(erichkeane::const_expr_string::size_type i) const 116 | { 117 | return i < ctor1.size() ? 118 | ( 119 | ctor1.at(i) != root[i] ? false : helper(i+1) 120 | ) 121 | : true; 122 | } 123 | 124 | constexpr bool operator()() const 125 | { 126 | return helper(0); 127 | } 128 | }; 129 | 130 | struct test_data 131 | { 132 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 133 | static constexpr erichkeane::const_expr_string ctor1 = root; 134 | 135 | constexpr bool operator()() const 136 | { 137 | static_assert(ctor1.data() == root, "test_data"); 138 | return true; 139 | } 140 | }; 141 | 142 | struct test_base_compare 143 | { 144 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 145 | static constexpr erichkeane::const_expr_string ctor1 = root; 146 | static constexpr const char root2[] = "asdfqefaxdvaqwerasdf"; 147 | static constexpr erichkeane::const_expr_string ctor2 = root2; 148 | static constexpr const char root3[] = "asdfqefaxdvaqwerasds"; 149 | static constexpr erichkeane::const_expr_string ctor3 = root3; 150 | static constexpr const char root4[] = "asdfqefaxdvaqwerasdc"; 151 | static constexpr erichkeane::const_expr_string ctor4 = root4; 152 | static constexpr const char root5[] = "qefax"; 153 | static constexpr erichkeane::const_expr_string ctor5 = root5; 154 | 155 | constexpr bool operator()() const 156 | { 157 | // Compare-all 158 | static_assert(ctor1.compare(ctor2) == 0, " failed equal"); 159 | static_assert(ctor1.compare(ctor3) != 0, " failed not equal"); 160 | static_assert(ctor1.compare(ctor3) < 0, " failed lt"); 161 | static_assert(ctor1.compare(ctor3) <= 0, " failed lte"); 162 | static_assert(ctor1.compare(ctor2) <= 0, " failed lte"); 163 | static_assert(ctor1.compare(ctor4) > 0, " failed gt"); 164 | static_assert(ctor1.compare(ctor4) >= 0, " failed gte"); 165 | static_assert(ctor1.compare(ctor2) >= 0, " failed gte"); 166 | 167 | // compare first-range-only 168 | static_assert(ctor1.compare(4, 5, ctor5) == 0, "failed equal"); 169 | static_assert(ctor1.compare(10, 5, ctor5) != 0, "failed not equal"); 170 | static_assert(ctor1.compare(10, 4, ctor5) < 0, "failed lt"); 171 | static_assert(ctor1.compare(10, 4, ctor5) <= 0, "failed lte"); 172 | static_assert(ctor1.compare(10, 7, ctor5) > 0, "failed gt"); 173 | static_assert(ctor1.compare(10, 7, ctor5) >= 0, "failed gte"); 174 | static_assert(ctor1.compare(5, 5, ctor5) < 0, "failed lt"); 175 | static_assert(ctor1.compare(8, 5, ctor5) > 0, "failed gt"); 176 | 177 | // compare full ranges 178 | static_assert(ctor1.compare(4, 5, ctor5, 0, 5) == 0, "failed equal"); 179 | static_assert(ctor1.compare(4, 5, ctor5, 0, 4) > 0, "failed gt"); 180 | static_assert(ctor1.compare(4, 4, ctor5, 0, 5) < 0, "failed lt"); 181 | static_assert(ctor1.compare(6, 3, ctor5, 2, 3) == 0, "failed equal"); 182 | static_assert(ctor1.compare(10, 3, ctor5, 2, 3) != 0, "failed not equal"); 183 | 184 | // Compare-all with char ptrs 185 | static_assert(ctor1.compare("asdfqefaxdvaqwerasdf") == 0, " failed equal"); 186 | static_assert(ctor1.compare("asdfqdfaxdvaqwerasdf") != 0, " failed not equal"); 187 | static_assert(ctor1.compare("asdfqffaxdvaqwerasdf") < 0, " failed lt"); 188 | static_assert(ctor1.compare("asdfqdfaxdvaqwerasdf") > 0, " failed gt"); 189 | 190 | // compare first-range-only with char ptrs 191 | static_assert(ctor1.compare(4, 5, "qefax") == 0, "failed equal"); 192 | static_assert(ctor1.compare(10, 5, "qefax") != 0, "failed not equal"); 193 | static_assert(ctor1.compare(10, 4, "qefax") < 0, "failed lt"); 194 | static_assert(ctor1.compare(10, 4, "qefax") <= 0, "failed lte"); 195 | static_assert(ctor1.compare(10, 7, "qefax") > 0, "failed gt"); 196 | static_assert(ctor1.compare(10, 7, "qefax") >= 0, "failed gte"); 197 | static_assert(ctor1.compare(5, 5, "qefax") < 0, "failed lt"); 198 | static_assert(ctor1.compare(8, 5, "qefax") > 0, "failed gt"); 199 | 200 | // compare full ranges with char ptrs 201 | static_assert(ctor1.compare(4, 5, "qefax", 5) == 0, "failed equal"); 202 | static_assert(ctor1.compare(4, 5, "qefax", 4) > 0, "failed gt"); 203 | static_assert(ctor1.compare(4, 4, "qefax", 5) < 0, "failed lt"); 204 | static_assert(ctor1.compare(6, 3, "fax", 3) == 0, "failed equal"); 205 | static_assert(ctor1.compare(10, 3, "fax", 3) != 0, "failed not equal"); 206 | return true; 207 | } 208 | 209 | }; 210 | 211 | struct test_compare_operators 212 | { 213 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 214 | static constexpr erichkeane::const_expr_string ctor1 = root; 215 | static constexpr const char root2[] = "asdfqefaxdvaqwerasdf"; 216 | static constexpr erichkeane::const_expr_string ctor2 = root2; 217 | static constexpr const char root3[] = "asdfqefaxdvaqwerasds"; 218 | static constexpr erichkeane::const_expr_string ctor3 = root3; 219 | static constexpr const char root4[] = "asdfqefaxdvaqwerasdc"; 220 | static constexpr erichkeane::const_expr_string ctor4 = root4; 221 | 222 | constexpr bool operator()() const 223 | { 224 | static_assert(ctor1 == ctor2, " failed equal"); 225 | static_assert(ctor1 != ctor3, " failed not equal"); 226 | static_assert(ctor1 < ctor3, " failed lt"); 227 | static_assert(ctor1 <= ctor3, " failed lte"); 228 | static_assert(ctor1 <= ctor2, " failed lte"); 229 | static_assert(ctor1 > ctor4, " failed gt"); 230 | static_assert(ctor1 >= ctor4, " failed gte"); 231 | static_assert(ctor1 >= ctor2, " failed gte"); 232 | return true; 233 | } 234 | }; 235 | 236 | struct test_front_back 237 | { 238 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 239 | static constexpr erichkeane::const_expr_string ctor1 = root; 240 | 241 | constexpr bool operator()() const 242 | { 243 | static_assert(ctor1.front() == root[0], "front"); 244 | static_assert(ctor1.back() == root[ctor1.size() - 1], "back"); 245 | static_assert(&ctor1.front() == &root[0], "front addr"); 246 | static_assert(&ctor1.back() == &root[ctor1.size() - 1], "back addr"); 247 | 248 | return true; 249 | } 250 | }; 251 | 252 | struct test_max_size 253 | { 254 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 255 | static constexpr erichkeane::const_expr_string ctor1 = root; 256 | 257 | constexpr bool operator()() const 258 | { 259 | static_assert(ctor1.max_size() == size_t(-1) - 1, "Failed max size"); 260 | 261 | return true; 262 | } 263 | }; 264 | 265 | struct test_empty 266 | { 267 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 268 | static constexpr erichkeane::const_expr_string ctor1 = root; 269 | static constexpr erichkeane::const_expr_string ctor2 = ""; 270 | 271 | constexpr bool operator()() const 272 | { 273 | static_assert(!ctor1.empty(), "failed empty"); 274 | static_assert(ctor2.empty(), "failed empty"); 275 | 276 | return true; 277 | } 278 | }; 279 | 280 | bool test_ostream() 281 | { 282 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 283 | static constexpr erichkeane::const_expr_string ctor1 = root; 284 | 285 | std::stringstream ss; 286 | ss << ctor1; 287 | assert(ss.str() == root); 288 | return true; 289 | } 290 | 291 | struct test_find 292 | { 293 | static constexpr const char root[] = "asdfqefaxdvaqwerasdf"; 294 | static constexpr erichkeane::const_expr_string ctor1 = root; 295 | static constexpr const char root2[] = "axd"; 296 | static constexpr erichkeane::const_expr_string ctor2 = root2; 297 | static constexpr const char root3[] = "df"; 298 | static constexpr erichkeane::const_expr_string ctor3 = root3; 299 | 300 | constexpr bool operator()() const 301 | { 302 | static_assert(ctor1.find(ctor2, 2) == 7, "find"); 303 | static_assert(ctor1.find('q', 1) == 4, "find"); 304 | static_assert(ctor1.find("axd", 3) == 7, "find"); 305 | static_assert(ctor1.find("axd", 3, 3) == 7, "find"); 306 | 307 | static_assert(ctor1.rfind(ctor3) == 18, "rfind"); 308 | static_assert(ctor1.rfind('d') == 18, "rfind"); 309 | static_assert(ctor1.rfind("df") == 18, "rfind"); 310 | static_assert(ctor1.rfind("dfe", ctor1.size(), 2) == 18, "rfind"); 311 | 312 | static_assert(ctor1.find_first_of(ctor2) == 0, "find_first_of"); 313 | static_assert(ctor1.find_first_of(ctor2, 1) == 2, "find_first_of"); 314 | static_assert(ctor1.find_first_of('f') == 3, "find_first_of"); 315 | static_assert(ctor1.find_first_of("efa", 1) == 3, "find_first_of"); 316 | static_assert(ctor1.find_first_of("efa", 1, 2) == 3, "find_first_of"); 317 | 318 | static_assert(ctor1.find_last_of(ctor2) == 18, "find_last_of"); 319 | static_assert(ctor1.find_last_of(ctor2, 17) == 16, "find_last_of"); 320 | static_assert(ctor1.find_last_of('f') == 19, "find_last_of"); 321 | static_assert(ctor1.find_last_of("efa", 17) == 16, "find_last_of"); 322 | static_assert(ctor1.find_last_of("efa", 4, 2) == 3, "find_last_of"); 323 | 324 | static_assert(ctor1.find_first_not_of(ctor2) == 1, "find_first_not_of"); 325 | static_assert(ctor1.find_first_not_of(ctor2, 2) == 3, "find_first_not_of"); 326 | static_assert(ctor1.find_first_not_of('a') == 1, "find_first_not_of"); 327 | static_assert(ctor1.find_first_not_of("efa", 2) == 2, "find_first_not_of"); 328 | static_assert(ctor1.find_first_not_of("efa", 2, 2) == 2, "find_first_not_of"); 329 | 330 | static_assert(ctor1.find_last_not_of(ctor2) == 19, "find_last_not_of"); 331 | static_assert(ctor1.find_last_not_of(ctor2, 16) == 15, "find_last_not_of"); 332 | static_assert(ctor1.find_last_not_of('f') == 18, "find_last_not_of"); 333 | static_assert(ctor1.find_last_not_of("efa", 16) == 15, "find_last_not_of"); 334 | static_assert(ctor1.find_last_not_of("efa", 3, 2) == 2, "find_last_not_of"); 335 | return true; 336 | } 337 | }; 338 | 339 | int main() 340 | { 341 | static_assert(test_ctors{}(), "failed"); 342 | static_assert(test_char_conversion{}(), "failed"); 343 | static_assert(test_sizes{}(), "failed"); 344 | static_assert(test_iterators{}(), "failed"); 345 | static_assert(test_const_iterators{}(), "failed"); 346 | static_assert(test_index_op{}(), "failed"); 347 | static_assert(test_at{}(), "failed"); 348 | static_assert(test_data{}(), "failed"); 349 | static_assert(test_base_compare{}(), "failed"); 350 | static_assert(test_empty{}(), "failed"); 351 | static_assert(test_front_back{}(), "failed"); 352 | static_assert(test_max_size{}(), "failed"); 353 | static_assert(test_compare_operators{}(), "failed"); 354 | assert(test_ostream()); 355 | static_assert(test_find{}(), "failed"); 356 | } 357 | -------------------------------------------------------------------------------- /include/const_expr_string.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace erichkeane 7 | { 8 | // Definition 9 | template 10 | class const_expr_string 11 | { 12 | public: 13 | using value_type = CharT; 14 | using traits_type = std::char_traits; 15 | using size_type = size_t; 16 | using difference_type = std::ptrdiff_t; 17 | using const_pointer = const CharT*; 18 | using pointer = const_pointer; 19 | using const_reference = const CharT&; 20 | using reference = const_reference; 21 | using const_iterator = const CharT*; 22 | using iterator = const_iterator; 23 | static constexpr size_type npos = size_type(-1); 24 | 25 | // Constructors 26 | constexpr const_expr_string(const_pointer data) noexcept; 27 | constexpr const_expr_string(const const_expr_string& other) = default; 28 | 29 | // Size based 30 | constexpr size_t length() const noexcept; 31 | constexpr size_t size() const noexcept; 32 | constexpr size_type max_size() const noexcept; 33 | constexpr bool empty() const noexcept; 34 | 35 | // Conversion operator 36 | constexpr operator const_pointer() const noexcept; 37 | 38 | // Iterators 39 | constexpr iterator begin() const noexcept; 40 | constexpr const_iterator cbegin() const noexcept; 41 | constexpr iterator end() const noexcept; 42 | constexpr const_iterator cend() const noexcept; 43 | 44 | // Indexing 45 | constexpr const_reference operator[](size_type pos) const; 46 | constexpr const_reference at(size_type pos) const; 47 | 48 | // ref positions 49 | constexpr const_reference front() const; 50 | constexpr const_reference back() const; 51 | 52 | // Data member 53 | constexpr const_pointer data() const noexcept; 54 | 55 | // Compares 56 | constexpr int compare(const_expr_string v) const; 57 | constexpr int compare(size_type pos1, size_type count1, const_expr_string v) const; 58 | constexpr int compare(size_type pos1, size_type count1, const_expr_string v, 59 | size_type pos2, size_type count2) const; 60 | constexpr int compare(const_pointer s) const; 61 | constexpr int compare(size_type pos1, size_type count1, const_pointer s) const; 62 | constexpr int compare(size_type pos1, size_type count1, const_pointer s, size_type count2) const; 63 | 64 | // Finds 65 | constexpr size_type find(const_expr_string v, size_type pos = 0) const; 66 | constexpr size_type find(value_type c, size_type pos = 0) const; 67 | constexpr size_type find(const_pointer, size_type pos = 0) const; 68 | constexpr size_type find(const_pointer, size_type pos, size_type count) const; 69 | 70 | constexpr size_type rfind(const_expr_string v, size_type pos = npos) const; 71 | constexpr size_type rfind(value_type c, size_type pos = npos) const; 72 | constexpr size_type rfind(const_pointer, size_type pos = npos) const; 73 | constexpr size_type rfind(const_pointer, size_type pos, size_type count) const; 74 | 75 | constexpr size_type find_first_of(const_expr_string v, size_type pos = 0) const; 76 | constexpr size_type find_first_of(value_type c, size_type pos = 0) const; 77 | constexpr size_type find_first_of(const_pointer, size_type pos = 0) const; 78 | constexpr size_type find_first_of(const_pointer, size_type pos, size_type count) const; 79 | 80 | constexpr size_type find_last_of(const_expr_string v, size_type pos = npos) const; 81 | constexpr size_type find_last_of(value_type c, size_type pos = npos) const; 82 | constexpr size_type find_last_of(const_pointer, size_type pos = npos) const; 83 | constexpr size_type find_last_of(const_pointer, size_type pos, size_type count) const; 84 | 85 | constexpr size_type find_first_not_of(const_expr_string v, size_type pos = 0) const; 86 | constexpr size_type find_first_not_of(value_type c, size_type pos = 0) const; 87 | constexpr size_type find_first_not_of(const_pointer, size_type pos = 0) const; 88 | constexpr size_type find_first_not_of(const_pointer, size_type pos, size_type count) const; 89 | 90 | constexpr size_type find_last_not_of(const_expr_string v, size_type pos = npos) const; 91 | constexpr size_type find_last_not_of(value_type c, size_type pos = npos) const; 92 | constexpr size_type find_last_not_of(const_pointer, size_type pos = npos) const; 93 | constexpr size_type find_last_not_of(const_pointer, size_type pos, size_type count) const; 94 | private: 95 | const_pointer _data; 96 | static constexpr size_type str_len(const_pointer str); 97 | 98 | template 99 | constexpr size_type find_base(size_type initPos, 100 | size_type termVal, TermCmp termcmp, 101 | Mut mut, SucCmp cmp) const; 102 | }; 103 | } 104 | 105 | #include "const_expr_string_helper_defs.hpp" 106 | #include "const_expr_string_universal.hpp" 107 | #if __cpp_constexpr >=201304 108 | #include "const_expr_string_cpp14.hpp" 109 | #else 110 | #include "const_expr_string_cpp11.hpp" 111 | #endif 112 | -------------------------------------------------------------------------------- /include/const_expr_string_cpp11.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace erichkeane 3 | { 4 | template 5 | constexpr typename const_expr_string::size_type 6 | const_expr_string::str_len(typename const_expr_string::const_pointer str) 7 | { 8 | return str[0] =='\0' ? 0 : 1 + str_len(str+1); 9 | } 10 | 11 | template template 12 | constexpr typename const_expr_string::size_type 13 | const_expr_string::find_base( 14 | typename const_expr_string::size_type initPos, 15 | typename const_expr_string::size_type termVal, TermCmp termcmp, 16 | Mut mut, SucCmp cmp) const 17 | { 18 | return termcmp(initPos, termVal) ? 19 | ( 20 | cmp(_data + initPos) ? initPos : 21 | find_base(mut(initPos,1), termVal, termcmp, mut, cmp) 22 | ) 23 | : npos;// termcmp 24 | } 25 | 26 | template 27 | constexpr bool is_one_of_helper( 28 | const const_expr_string& chars, 29 | const CharT* lhs, 30 | typename const_expr_string::size_type index, 31 | typename const_expr_string::size_type count) 32 | { 33 | return index == count ? false : 34 | ( 35 | chars[index] == *lhs ? true : is_one_of_helper(chars, lhs, index +1, count) 36 | ); 37 | } 38 | 39 | template 40 | constexpr bool is_one_of::operator()(const CharT* lhs) const 41 | { 42 | return is_one_of_helper(_c, lhs, 0, _c.size()); 43 | } 44 | 45 | template 46 | constexpr bool is_one_of_partial::operator()(const CharT* lhs) const 47 | { 48 | return is_one_of_helper(_c, lhs, 0, _count); 49 | } 50 | 51 | template 52 | constexpr bool is_not_one_of_helper( 53 | const const_expr_string& chars, 54 | const CharT* lhs, 55 | typename const_expr_string::size_type index, 56 | typename const_expr_string::size_type count) 57 | { 58 | return index == count ? true : 59 | ( 60 | chars[index] == *lhs ? false : is_not_one_of_helper(chars, lhs, index +1, count) 61 | ); 62 | } 63 | 64 | template 65 | constexpr bool is_not_one_of::operator()(const CharT* lhs) const 66 | { 67 | return is_not_one_of_helper(_c, lhs, 0, _c.size()); 68 | } 69 | 70 | template 71 | constexpr bool is_not_one_of_partial::operator()(const CharT* lhs) const 72 | { 73 | return is_not_one_of_helper(_c, lhs, 0, _count); 74 | } 75 | 76 | template 77 | constexpr int compare_helper( 78 | const const_expr_string& lhs, 79 | typename const_expr_string::size_type pos1, 80 | typename const_expr_string::size_type count1, 81 | const const_expr_string& rhs, 82 | typename const_expr_string::size_type pos2, 83 | typename const_expr_string::size_type index 84 | ) 85 | { 86 | return count1 == index ? 0 : 87 | ( 88 | lhs.data()[index + pos1] == rhs.data()[index + pos2] ? 89 | compare_helper(lhs, pos1, count1, rhs, pos2, index + 1) : 90 | ( 91 | lhs.data()[index + pos1] < rhs.data()[index + pos2] ? -1 : 1 92 | ) 93 | ); 94 | } 95 | template 96 | constexpr int const_expr_string::compare(typename const_expr_string::size_type pos1, 97 | typename const_expr_string::size_type count1, 98 | typename const_expr_string::const_expr_string v, 99 | typename const_expr_string::size_type pos2, 100 | typename const_expr_string::size_type count2) const 101 | { 102 | return count1 < count2 ? -1 : 103 | ( 104 | count1 > count2 ? 1 : 105 | compare_helper(*this, pos1, count1, v, pos2, 0) 106 | ); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /include/const_expr_string_cpp14.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace erichkeane 3 | { 4 | template 5 | constexpr typename const_expr_string::size_type 6 | const_expr_string::str_len(const_expr_string::const_pointer str) 7 | { 8 | size_type i = 0; 9 | while (str[i] != '\0') ++i; 10 | return i; 11 | } 12 | 13 | template template 14 | constexpr typename const_expr_string::size_type 15 | const_expr_string::find_base( 16 | const_expr_string::size_type initPos, 17 | const_expr_string::size_type termVal, TermCmp termcmp, 18 | Mut mut, SucCmp cmp) const 19 | { 20 | for (;termcmp(initPos, termVal); initPos = mut(initPos, 1)) 21 | { 22 | if(cmp(_data + initPos)) 23 | { 24 | return initPos; 25 | } 26 | } 27 | 28 | return npos; 29 | } 30 | 31 | template 32 | constexpr bool is_one_of::operator()(const CharT* lhs) const 33 | { 34 | for (auto&& itr : _c) 35 | { 36 | if (itr == *lhs) 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | template 43 | constexpr bool is_one_of_partial::operator()(const CharT* lhs) const 44 | { 45 | for (typename const_expr_string::size_type i = 0; 46 | i < _count; ++i) 47 | { 48 | if (*lhs == _c[i]) 49 | return true; 50 | } 51 | return false; 52 | } 53 | 54 | template 55 | constexpr bool is_not_one_of::operator()(const CharT* lhs) const 56 | { 57 | for (auto&& itr : _c) 58 | { 59 | if (itr == *lhs) 60 | return false; 61 | } 62 | return true; 63 | } 64 | 65 | template 66 | constexpr bool is_not_one_of_partial::operator()(const CharT* lhs) const 67 | { 68 | for (typename const_expr_string::size_type i = 0; 69 | i < _count; ++i) 70 | { 71 | if (*lhs == _c[i]) 72 | return false; 73 | } 74 | return true; 75 | } 76 | 77 | template 78 | constexpr int const_expr_string::compare(typename const_expr_string::size_type pos1, 79 | typename const_expr_string::size_type count1, 80 | typename const_expr_string::const_expr_string v, 81 | const_expr_string::size_type pos2, 82 | const_expr_string::size_type count2) const 83 | { 84 | typename const_expr_string::size_type lhsSize = count1; 85 | typename const_expr_string::size_type rhsSize = count2; 86 | 87 | if (lhsSize < rhsSize) return -1; 88 | if (lhsSize > rhsSize) return 1; 89 | 90 | for (const_expr_string::size_type i = 0; i < lhsSize; ++i) 91 | { 92 | if (_data[i + pos1] < v._data[i + pos2]) return -1; 93 | if (_data[i + pos1] > v._data[i + pos2]) return 1; 94 | } 95 | 96 | return 0; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /include/const_expr_string_helper_defs.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // contains a number of helper definitions. most of these can be replaced once the standard library 4 | // makes the standard version constexpr 5 | namespace erichkeane 6 | { 7 | template 8 | struct char_equal 9 | { 10 | CharT _c; 11 | constexpr char_equal(CharT c): _c(c){} 12 | constexpr bool operator()(const CharT* lhs) const; 13 | }; 14 | 15 | template 16 | struct char_not_equal 17 | { 18 | CharT _c; 19 | constexpr char_not_equal(CharT c): _c(c){} 20 | constexpr bool operator()(const CharT* lhs) const; 21 | }; 22 | 23 | template 24 | struct ces_equal 25 | { 26 | const_expr_string _c; 27 | typename const_expr_string::size_type _count; 28 | 29 | constexpr ces_equal(const_expr_string c): _c(c), _count(c.size()){} 30 | constexpr ces_equal(const_expr_string c, 31 | typename const_expr_string::size_type count): _c(c), _count(count){} 32 | 33 | constexpr bool operator()(const CharT* lhs) const; 34 | }; 35 | 36 | template 37 | struct is_one_of 38 | { 39 | const_expr_string _c; 40 | 41 | constexpr is_one_of(const_expr_string c): _c(c){} 42 | 43 | constexpr bool operator()(const CharT* lhs) const; 44 | }; 45 | 46 | template 47 | struct is_not_one_of 48 | { 49 | const_expr_string _c; 50 | 51 | constexpr is_not_one_of(const_expr_string c): _c(c){} 52 | 53 | constexpr bool operator()(const CharT* lhs) const; 54 | }; 55 | 56 | template 57 | struct is_one_of_partial 58 | { 59 | const_expr_string _c; 60 | typename const_expr_string::size_type _count; 61 | 62 | 63 | constexpr is_one_of_partial(const_expr_string c, 64 | typename const_expr_string::size_type count): _c(c), _count(count){} 65 | 66 | 67 | constexpr bool operator()(const CharT* lhs) const; 68 | }; 69 | 70 | template 71 | struct is_not_one_of_partial 72 | { 73 | const_expr_string _c; 74 | typename const_expr_string::size_type _count; 75 | 76 | 77 | constexpr is_not_one_of_partial(const_expr_string c, 78 | typename const_expr_string::size_type count): _c(c), _count(count){} 79 | 80 | constexpr bool operator()(const CharT* lhs) const; 81 | }; 82 | 83 | template 84 | struct less 85 | { 86 | using result_type = bool; 87 | using first_argument_type = T; 88 | using second_argument_type = T; 89 | 90 | constexpr result_type operator()( 91 | const first_argument_type& lhs, 92 | const second_argument_type& rhs) const; 93 | }; 94 | template 95 | struct greater_equal 96 | { 97 | using result_type = bool; 98 | using first_argument_type = T; 99 | using second_argument_type = T; 100 | 101 | constexpr result_type operator()( 102 | const first_argument_type& lhs, 103 | const second_argument_type& rhs) const; 104 | }; 105 | template 106 | struct plus 107 | { 108 | using first_argument_type = T; 109 | using second_argument_type = T; 110 | using result_type = decltype(first_argument_type(0) + second_argument_type(0)); 111 | 112 | constexpr result_type operator()( 113 | const first_argument_type& lhs, 114 | const second_argument_type& rhs) const; 115 | }; 116 | template 117 | struct minus 118 | { 119 | using first_argument_type = T; 120 | using second_argument_type = T; 121 | using result_type = decltype(first_argument_type(0) - second_argument_type(0)); 122 | 123 | constexpr result_type operator()( 124 | const first_argument_type& lhs, 125 | const second_argument_type& rhs) const; 126 | }; 127 | } 128 | -------------------------------------------------------------------------------- /include/const_expr_string_universal.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #include "const_expr_string_helper_defs.hpp" 5 | // File exists for definitions that are both C++11 and C++14 6 | namespace erichkeane 7 | { 8 | // Helper implementations 9 | template 10 | constexpr bool char_equal::operator()(const CharT* lhs) const 11 | { 12 | return *lhs == _c; 13 | } 14 | 15 | template 16 | constexpr bool char_not_equal::operator()(const CharT* lhs) const 17 | { 18 | return *lhs != _c; 19 | } 20 | 21 | template 22 | constexpr bool ces_equal::operator()(const CharT* lhs) const 23 | { 24 | return 0 == const_expr_string(lhs).compare(0, _count, _c, 0, _count); 25 | } 26 | 27 | template 28 | constexpr typename less::result_type less::operator()( 29 | const first_argument_type& lhs, 30 | const second_argument_type& rhs) const 31 | { 32 | return lhs < rhs; 33 | } 34 | 35 | template 36 | constexpr typename greater_equal::result_type greater_equal::operator()( 37 | const first_argument_type& lhs, 38 | const second_argument_type& rhs) const 39 | { 40 | return lhs >= rhs; 41 | } 42 | 43 | template 44 | constexpr typename plus::result_type plus::operator()( 45 | const first_argument_type& lhs, 46 | const second_argument_type& rhs) const 47 | { 48 | return lhs + rhs; 49 | } 50 | 51 | template 52 | constexpr typename minus::result_type minus::operator()( 53 | const first_argument_type& lhs, 54 | const second_argument_type& rhs) const 55 | { 56 | return lhs - rhs; 57 | } 58 | // Member Implementations 59 | template 60 | constexpr const_expr_string::const_expr_string( 61 | typename const_expr_string::const_pointer data) noexcept 62 | : _data(data){} 63 | 64 | template 65 | constexpr const_expr_string::operator 66 | typename const_expr_string::const_pointer() const noexcept 67 | { 68 | return _data; 69 | } 70 | 71 | template 72 | constexpr size_t const_expr_string::length() const noexcept 73 | { 74 | return const_expr_string::str_len(_data); 75 | } 76 | 77 | template 78 | constexpr size_t const_expr_string::size() const noexcept 79 | { 80 | return length(); 81 | } 82 | 83 | template 84 | constexpr typename const_expr_string::iterator const_expr_string::begin() const noexcept 85 | { 86 | return _data; 87 | } 88 | 89 | template 90 | constexpr typename const_expr_string::const_iterator const_expr_string::cbegin() const noexcept 91 | { 92 | return begin(); 93 | } 94 | template 95 | constexpr typename const_expr_string::iterator const_expr_string::end() const noexcept 96 | { 97 | return _data + size(); 98 | } 99 | 100 | template 101 | constexpr typename const_expr_string::const_iterator const_expr_string::cend() const noexcept 102 | { 103 | return end(); 104 | } 105 | 106 | template 107 | constexpr typename const_expr_string::const_reference const_expr_string::operator[](typename const_expr_string::size_type pos) const 108 | { 109 | return _data[pos]; 110 | } 111 | 112 | template 113 | constexpr typename const_expr_string::const_reference const_expr_string::at(typename const_expr_string::size_type pos) const 114 | { 115 | return pos >= size() ? 116 | // GCC complains that this throw returns a temporary, 117 | // so the comma and operator[](0) is to silence that 118 | throw std::out_of_range("call to at with too large index"), operator[](0) : 119 | operator[](pos); 120 | } 121 | 122 | template 123 | constexpr typename const_expr_string::const_pointer const_expr_string::data() const noexcept 124 | { 125 | return _data; 126 | } 127 | 128 | template 129 | constexpr typename const_expr_string::const_reference const_expr_string::front() const 130 | { 131 | return operator[](0); 132 | } 133 | 134 | template 135 | constexpr typename const_expr_string::const_reference const_expr_string::back() const 136 | { 137 | return operator[](size() - 1); 138 | } 139 | 140 | template 141 | constexpr typename const_expr_string::size_type const_expr_string::max_size() const noexcept 142 | { 143 | // reserves npos to be an illegal index 144 | return std::numeric_limits::max() - 1; 145 | } 146 | 147 | template 148 | constexpr bool const_expr_string::empty() const noexcept 149 | { 150 | return _data[0] == '\0'; 151 | } 152 | template 153 | constexpr int const_expr_string::compare(const_expr_string v) const 154 | { 155 | return compare(0, size(), v, 0, size()); 156 | } 157 | 158 | template 159 | constexpr int const_expr_string::compare(typename const_expr_string::size_type pos1, 160 | typename const_expr_string::size_type count1, 161 | typename const_expr_string::const_expr_string v) const 162 | { 163 | return compare (pos1, count1, v, 0, v.size()); 164 | } 165 | 166 | template 167 | constexpr int const_expr_string::compare( 168 | typename const_expr_string::const_pointer s) const 169 | { 170 | return compare(0, size(), s, str_len(s)); 171 | } 172 | 173 | template 174 | constexpr int const_expr_string::compare( 175 | typename const_expr_string::size_type pos1, 176 | typename const_expr_string::size_type count1, 177 | typename const_expr_string::const_pointer s) const 178 | { 179 | return compare(pos1, count1, s, str_len(s)); 180 | } 181 | 182 | template 183 | constexpr int const_expr_string::compare( 184 | typename const_expr_string::size_type pos1, 185 | typename const_expr_string::size_type count1, 186 | typename const_expr_string::const_pointer s, 187 | typename const_expr_string::size_type count2) const 188 | { 189 | return compare(pos1, count1, const_expr_string(s), 0, count2); 190 | } 191 | 192 | // compare operators 193 | template 194 | constexpr bool operator==(const_expr_string lhs, const_expr_string rhs) 195 | { 196 | return lhs.compare(rhs) == 0; 197 | } 198 | 199 | template 200 | constexpr bool operator!=(const_expr_string lhs, const_expr_string rhs) 201 | { 202 | return !(lhs == rhs); 203 | } 204 | 205 | template 206 | constexpr bool operator<(const_expr_string lhs, const_expr_string rhs) 207 | { 208 | return lhs.compare(rhs) < 0; 209 | } 210 | 211 | template 212 | constexpr bool operator<=(const_expr_string lhs, const_expr_string rhs) 213 | { 214 | return lhs.compare(rhs) <= 0; 215 | } 216 | 217 | template 218 | constexpr bool operator>(const_expr_string lhs, const_expr_string rhs) 219 | { 220 | return lhs.compare(rhs) > 0; 221 | } 222 | 223 | template 224 | constexpr bool operator>=(const_expr_string lhs, const_expr_string rhs) 225 | { 226 | return lhs.compare(rhs) >= 0; 227 | } 228 | 229 | // OStream conversion 230 | template 231 | std::basic_ostream& operator<<(std::basic_ostream& os, 232 | const_expr_string v) 233 | { 234 | return os << v.data(); 235 | } 236 | 237 | // Find methods 238 | template 239 | constexpr typename const_expr_string::size_type const_expr_string::find( 240 | const_expr_string v, 241 | typename const_expr_string::size_type pos) const 242 | { 243 | return find_base( 244 | pos, 245 | size(), less::size_type>(), 246 | plus::size_type>(), ces_equal(v)); 247 | } 248 | 249 | template 250 | constexpr typename const_expr_string::size_type const_expr_string::rfind( 251 | const_expr_string v, 252 | typename const_expr_string::size_type pos) const 253 | { 254 | return find_base( 255 | pos == npos ? size() - 1 : pos, 256 | 0, greater_equal::size_type>(), 257 | minus::size_type>(), ces_equal(v)); 258 | } 259 | 260 | template 261 | constexpr typename const_expr_string::size_type const_expr_string::find( 262 | typename const_expr_string::value_type c, 263 | typename const_expr_string::size_type pos) const 264 | { 265 | return find_base(pos, 266 | size(), less::size_type>(), 267 | plus::size_type>(), char_equal(c) 268 | ); 269 | } 270 | 271 | template 272 | constexpr typename const_expr_string::size_type const_expr_string::rfind( 273 | typename const_expr_string::value_type c, 274 | typename const_expr_string::size_type pos) const 275 | { 276 | return find_base( 277 | pos == npos ? size() - 1 : pos, 278 | 0, greater_equal::size_type>(), 279 | minus::size_type>(), char_equal(c) 280 | ); 281 | } 282 | 283 | 284 | template 285 | constexpr typename const_expr_string::size_type const_expr_string::find( 286 | typename const_expr_string::const_pointer v, 287 | typename const_expr_string::size_type pos) const 288 | { 289 | return find(const_expr_string(v), pos); 290 | } 291 | template 292 | constexpr typename const_expr_string::size_type const_expr_string::rfind( 293 | typename const_expr_string::const_pointer v, 294 | typename const_expr_string::size_type pos) const 295 | { 296 | return rfind(const_expr_string(v), pos); 297 | } 298 | 299 | template 300 | constexpr typename const_expr_string::size_type const_expr_string::find( 301 | typename const_expr_string::const_pointer v, 302 | typename const_expr_string::size_type pos, 303 | typename const_expr_string::size_type count) const 304 | { 305 | return find_base( 306 | pos, 307 | size(), less::size_type>(), 308 | plus::size_type>(), 309 | ces_equal(const_expr_string{v}, count)); 310 | } 311 | template 312 | constexpr typename const_expr_string::size_type const_expr_string::rfind( 313 | typename const_expr_string::const_pointer v, 314 | typename const_expr_string::size_type pos, 315 | typename const_expr_string::size_type count) const 316 | { 317 | return find_base( 318 | pos == npos ? size() - 1 : pos, 319 | 0, greater_equal::size_type>(), 320 | minus::size_type>(), 321 | ces_equal(const_expr_string{v}, count)); 322 | } 323 | template 324 | constexpr typename const_expr_string::size_type const_expr_string::find_first_of( 325 | const_expr_string v, 326 | typename const_expr_string::size_type pos) const 327 | { 328 | return find_base( 329 | pos, 330 | size(), 331 | less::size_type>(), 332 | plus::size_type>(), 333 | is_one_of(v)); 334 | } 335 | 336 | template 337 | constexpr typename const_expr_string::size_type const_expr_string::find_first_of( 338 | typename const_expr_string::value_type c, 339 | typename const_expr_string::size_type pos) const 340 | { 341 | return find_base( 342 | pos, 343 | size(), 344 | less::size_type>(), 345 | plus::size_type>(), 346 | char_equal(c)); 347 | } 348 | template 349 | constexpr typename const_expr_string::size_type const_expr_string::find_first_of( 350 | typename const_expr_string::const_pointer v, 351 | typename const_expr_string::size_type pos) const 352 | { 353 | return find_first_of(const_expr_string(v),pos); 354 | } 355 | template 356 | constexpr typename const_expr_string::size_type const_expr_string::find_first_of( 357 | typename const_expr_string::const_pointer v, 358 | typename const_expr_string::size_type pos, 359 | typename const_expr_string::size_type count) const 360 | { 361 | return find_base( 362 | pos, 363 | size(), 364 | less::size_type>(), 365 | plus::size_type>(), 366 | is_one_of_partial(const_expr_string(v),count)); 367 | } 368 | template 369 | constexpr typename const_expr_string::size_type const_expr_string::find_last_of( 370 | const_expr_string v, 371 | typename const_expr_string::size_type pos) const 372 | { 373 | return find_base( 374 | pos == npos ? size() - 1 : pos, 375 | 0, 376 | greater_equal::size_type>(), 377 | minus::size_type>(), 378 | is_one_of(v)); 379 | } 380 | template 381 | constexpr typename const_expr_string::size_type const_expr_string::find_last_of( 382 | typename const_expr_string::value_type c, 383 | typename const_expr_string::size_type pos) const 384 | { 385 | return find_base( 386 | pos == npos ? size() - 1 : pos, 387 | 0, 388 | greater_equal::size_type>(), 389 | minus::size_type>(), 390 | char_equal(c)); 391 | } 392 | template 393 | constexpr typename const_expr_string::size_type const_expr_string::find_last_of( 394 | typename const_expr_string::const_pointer v, 395 | typename const_expr_string::size_type pos) const 396 | { 397 | return find_last_of(const_expr_string(v), pos); 398 | } 399 | 400 | template 401 | constexpr typename const_expr_string::size_type const_expr_string::find_last_of( 402 | typename const_expr_string::const_pointer v, 403 | typename const_expr_string::size_type pos, 404 | typename const_expr_string::size_type count) const 405 | { 406 | return find_base( 407 | pos == npos ? size() - 1 : pos, 408 | 0, 409 | greater_equal::size_type>(), 410 | minus::size_type>(), 411 | is_one_of_partial(const_expr_string(v), count)); 412 | } 413 | template 414 | constexpr typename const_expr_string::size_type const_expr_string::find_first_not_of( 415 | const_expr_string v, 416 | typename const_expr_string::size_type pos) const 417 | { 418 | return find_base( 419 | pos, 420 | size(), 421 | less::size_type>(), 422 | plus::size_type>(), 423 | is_not_one_of(v)); 424 | } 425 | template 426 | constexpr typename const_expr_string::size_type const_expr_string::find_first_not_of( 427 | typename const_expr_string::value_type c, 428 | typename const_expr_string::size_type pos) const 429 | { 430 | return find_base( 431 | pos, 432 | size(), 433 | less::size_type>(), 434 | plus::size_type>(), 435 | char_not_equal(c)); 436 | } 437 | template 438 | constexpr typename const_expr_string::size_type const_expr_string::find_first_not_of( 439 | typename const_expr_string::const_pointer v, 440 | typename const_expr_string::size_type pos) const 441 | { 442 | return find_first_not_of(const_expr_string(v),pos); 443 | } 444 | 445 | template 446 | constexpr typename const_expr_string::size_type const_expr_string::find_first_not_of( 447 | typename const_expr_string::const_pointer v, 448 | typename const_expr_string::size_type pos, 449 | typename const_expr_string::size_type count) const 450 | { 451 | return find_base( 452 | pos, 453 | size(), 454 | less::size_type>(), 455 | plus::size_type>(), 456 | is_not_one_of_partial(const_expr_string(v), count)); 457 | } 458 | template 459 | constexpr typename const_expr_string::size_type const_expr_string::find_last_not_of( 460 | const_expr_string v, 461 | typename const_expr_string::size_type pos) const 462 | { 463 | return find_base( 464 | pos == npos ? size() - 1 : pos, 465 | 0, 466 | greater_equal::size_type>(), 467 | minus::size_type>(), 468 | is_not_one_of(v)); 469 | } 470 | template 471 | constexpr typename const_expr_string::size_type const_expr_string::find_last_not_of( 472 | typename const_expr_string::value_type c, 473 | typename const_expr_string::size_type pos) const 474 | { 475 | return find_base( 476 | pos == npos ? size() - 1 : pos, 477 | 0, 478 | greater_equal::size_type>(), 479 | minus::size_type>(), 480 | char_not_equal(c)); 481 | } 482 | template 483 | constexpr typename const_expr_string::size_type const_expr_string::find_last_not_of( 484 | typename const_expr_string::const_pointer v, 485 | typename const_expr_string::size_type pos) const 486 | { 487 | return find_last_not_of(const_expr_string(v), pos); 488 | } 489 | 490 | template 491 | constexpr typename const_expr_string::size_type const_expr_string::find_last_not_of( 492 | typename const_expr_string::const_pointer v, 493 | typename const_expr_string::size_type pos, 494 | typename const_expr_string::size_type count) const 495 | { 496 | return find_base( 497 | pos == npos ? size() - 1 : pos, 498 | 0, 499 | greater_equal::size_type>(), 500 | minus::size_type>(), 501 | is_not_one_of_partial(const_expr_string(v), count)); 502 | } 503 | 504 | } 505 | --------------------------------------------------------------------------------