initial upload
This commit is contained in:
104
src/ckitty/system/strings.hpp
Normal file
104
src/ckitty/system/strings.hpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
|
||||
#include <ckitty/memory/primitives.hpp>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <locale>
|
||||
|
||||
namespace ckitty {
|
||||
|
||||
namespace strings {
|
||||
|
||||
// trim spaces
|
||||
|
||||
inline void ltrim(std::string &s) {
|
||||
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
|
||||
[](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
}
|
||||
|
||||
inline void rtrim(std::string &s) {
|
||||
s.erase(std::find_if(s.rbegin(), s.rend(),
|
||||
[](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), s.end());
|
||||
}
|
||||
|
||||
inline void trim(std::string& str) {
|
||||
ltrim(str);
|
||||
rtrim(str);
|
||||
}
|
||||
|
||||
inline std::string trimc(const std::string& str) {
|
||||
std::string copy = str;
|
||||
trim(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
inline std::string ltrimc(const std::string& str) {
|
||||
std::string copy = str;
|
||||
ltrim(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
inline std::string rtrimc(const std::string& str) {
|
||||
std::string copy = str;
|
||||
rtrim(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a displayable representation of the character.
|
||||
*/
|
||||
inline std::string display(char c) {
|
||||
if(std::isprint(c)) return std::string(1, c);
|
||||
|
||||
char chars[] = {
|
||||
'0', '1', '2', '3',
|
||||
'4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B',
|
||||
'C', 'D', 'E', 'F'
|
||||
};
|
||||
|
||||
std::string str = "0x00";
|
||||
str[2] = chars[ (c >> 4) & 0xF ];
|
||||
str[3] = chars[ c & 0xF ];
|
||||
return str;
|
||||
}
|
||||
|
||||
inline int hexChar(char c) {
|
||||
if('0' <= c && c <= '9') return c - '0';
|
||||
if('a' <= c && c <= 'f') return c - 'a' + 10;
|
||||
if('A' <= c && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// ascii upper & lower
|
||||
|
||||
inline void upper(std::string& str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
|
||||
}
|
||||
|
||||
inline void lower(std::string& str) {
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
||||
}
|
||||
|
||||
inline std::string copy_upper(const std::string& str) {
|
||||
std::string copy = str;
|
||||
upper(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
inline std::string copy_lower(const std::string& str) {
|
||||
std::string copy = str;
|
||||
lower(copy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user