Advertisement
Radfler

::ci_char_traits

Sep 18th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <cctype>
  2. #include <string>
  3.  
  4. struct ci_char_traits : public std::char_traits<char> {
  5.  
  6.     static constexpr char_type tolower(char_type symbol) {
  7.         return (symbol >= 65 && symbol <= 90) ? (symbol + 32) : symbol;
  8.     }
  9.  
  10.     static constexpr bool eq(char_type lhs, char_type rhs) noexcept {
  11.         return tolower(lhs) == tolower(rhs);
  12.     }
  13.  
  14.     static constexpr bool lt(char_type lhs, char_type rhs) noexcept {
  15.         return tolower(lhs) < tolower(rhs);
  16.     }
  17.  
  18.     static int compare(const char_type* lhs, const char_type* rhs, std::size_t size) {
  19.         while(size--) {
  20.             if(lt(*lhs, *rhs)) return -1;
  21.             if(lt(*rhs, *lhs)) return 1;
  22.             ++lhs;
  23.             ++rhs;
  24.         }
  25.         return 0;
  26.     }
  27.  
  28.     static const char_type* find(const char_type* string, std::size_t size, const char_type& symbol) {
  29.         while(size--) {
  30.             if(eq(*string, symbol)) {
  31.                 return string;
  32.             }
  33.             ++string;
  34.         }
  35.         return nullptr;
  36.     }
  37.  
  38. };
  39.  
  40. using ci_string = std::basic_string<char, ci_char_traits>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement