DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
ciString.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: ciString.hpp
5// Author: crdrisko
6// Date: 11/03/2020-07:26:03
7// Description: Defining a new char_traits class and associated utilities for case-insensitive string comparisons
8
9#ifndef DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_STRINGS_TRAITS_CISTRING_HPP
10#define DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_STRINGS_TRAITS_CISTRING_HPP
11
12#include <cstddef>
13#include <iostream>
14#include <string>
15
17{
22 struct ci_char_traits : public std::char_traits<char>
23 {
31 static constexpr char ct_toupper(char ch_) noexcept
32 {
33 if (ch_ >= 'a' && ch_ <= 'z')
34 return ch_ - 'a' + 'A';
35
36 return ch_;
37 }
38
49 static constexpr int compare(const char* s1_, const char* s2_, std::size_t count_) noexcept
50 {
51 while (count_-- != 0)
52 {
53 if (ct_toupper(*s1_) < ct_toupper(*s2_))
54 return -1;
55
56 if (ct_toupper(*s1_) > ct_toupper(*s2_))
57 return 1;
58
59 ++s1_;
60 ++s2_;
61 }
62
63 return 0;
64 }
65
76 static constexpr const char* find(const char* p_, std::size_t count_, const char& ch_) noexcept
77 {
78 const auto CH {ct_toupper(ch_)};
79
80 while (count_-- != 0)
81 {
82 if (ct_toupper(*p_) == CH)
83 return p_;
84
85 ++p_;
86 }
87
88 return nullptr;
89 }
90
91 static constexpr bool eq(char a, char b) noexcept { return ct_toupper(a) == ct_toupper(b); }
92 static constexpr bool lt(char a, char b) noexcept { return ct_toupper(a) < ct_toupper(b); }
93 };
94
95
103 template<class OutputTraits, class InputTraits>
104 constexpr std::basic_string<char, OutputTraits> traits_cast(const std::basic_string<char, InputTraits>& input) noexcept
105 {
106 std::basic_string<char, OutputTraits> ouput {input.data(), input.size()};
107 return ouput;
108 }
109
110
118 inline std::ostream& operator<<(std::ostream& os, const std::basic_string<char, ci_char_traits>& str)
119 {
120 os << str.c_str();
121 return os;
122 }
123
124
126 using ci_string = std::basic_string<char, ci_char_traits>;
127} // namespace CppUtils::Strings
128
129#endif
Definition ciString.hpp:17
std::basic_string< char, ci_char_traits > ci_string
Type alias for case-insensitive strings.
Definition ciString.hpp:126
std::ostream & operator<<(std::ostream &os, const std::basic_string< char, ci_char_traits > &str)
Definition ciString.hpp:118
constexpr std::basic_string< char, OutputTraits > traits_cast(const std::basic_string< char, InputTraits > &input) noexcept
Definition ciString.hpp:104
Definition ciString.hpp:23
static constexpr int compare(const char *s1_, const char *s2_, std::size_t count_) noexcept
Definition ciString.hpp:49
static constexpr bool lt(char a, char b) noexcept
Definition ciString.hpp:92
static constexpr char ct_toupper(char ch_) noexcept
Definition ciString.hpp:31
static constexpr const char * find(const char *p_, std::size_t count_, const char &ch_) noexcept
Definition ciString.hpp:76
static constexpr bool eq(char a, char b) noexcept
Definition ciString.hpp:91