Skip to content

Instantly share code, notes, and snippets.

@shotadft
Last active January 8, 2025 08:00
Show Gist options
  • Select an option

  • Save shotadft/0066f002abc587828e98ed039c4bdeb2 to your computer and use it in GitHub Desktop.

Select an option

Save shotadft/0066f002abc587828e98ed039c4bdeb2 to your computer and use it in GitHub Desktop.
C++のstring型にC#のstring型関連の関数をstringEx型として追加します。(要C++ 17以上)(未完成)
/**
* @file stringEx.h
* @brief The additional stringEx type extends the string type and adds various functions.
* @author Shotadft
* @license MIT License, Copyright 2024 Shotadft.
* @date 2024:08:03::13:03
*/
#ifndef STRING_EX_H
#define STRING_EX_H
#include <iostream>
#include <vector>
#include <string>
#include <regex>
/**
* @namespace StringEx
* @brief The namespace StringEx extends the string type and adds various functions.
*/
namespace StringEx {
using namespace std;
struct StringEx : public std::string
{
private:
// Stored value
string vstr;
/**
* @param (str) String
* @return True if nullptr, else false
*/
static inline bool isNullPointer(const string* str) {
return str == nullptr;
}
public:
StringEx() : string() {}
StringEx(const char* str) : string(str) { vstr = str; }
StringEx(const string str) : string(str) { vstr = str; }
/**
* @param (str) String
* @return True if null or empty, else false
*/
static inline bool isNullorEmpty(const string& str) {
return (isNullPointer(&str) || str == "\0");
}
/**
* @param (str) String
* @return True if null or Whitespace, else false
*/
static inline bool isNullorWhiteSpace(const string& str) {
return (isNullPointer(&str) || str == " " || str == " " || str == "\t");
}
/**
* @param (str) String
* @param (prefix) Prefix of the string to be searched
* @return True if the string starts at the prefix value, else false
*/
static inline bool startsWith(const string& str, const string& prefix) {
return str.size() >= prefix.size() && equal(prefix.begin(), prefix.end(), str.begin());
}
/**
* @param (str) String
* @param (pattern) Regular expression to adapt to the string to be searched
* @return True if the string starts at the pattern regex, else false
*/
static inline bool startsWith(const string& str, const regex& pattern)
{
smatch match;
return regex_search(str, match, pattern) && match.position() == 0;
}
/**
* @param (str) String
* @param (suffix) Suffix of the string to be searched
* @return True if the string starts at the suffix value, else false
*/
static inline bool endsWith(const string& str, const string& suffix) {
return str.size() >= suffix.size() && equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
/**
* @param (str) String
* @param (pattern) Regular expression to adapt to the string to be searched
* @return True if the string starts at the pattern regex, else false
*/
static inline bool endsWith(const string& str, const regex& pattern)
{
smatch match;
return regex_search(str, match, pattern) && (match.position() + match.length() == str.size());
}
/**
* @param (str) String
* @param (search) Char
* @return Returns the position of the character to be searched for as a number. Returns -1 if not found.
*/
static inline int indexOf(const string& str, char search) {
size_t pos = str.find(search);
if (pos != string::npos) {
return static_cast<int>(pos);
}
else
return -1;
}
/**
* @param (str) String
* @param (delim) String
* @brief Splits a string.
*/
static inline vector<string> split(const string& str, char delim) {
vector<string> tokens;
string token;
for (char ch : str) {
if (ch == delim) {
tokens.push_back(move(token));
token.clear();
}
else {
token += ch;
}
}
if (!token.empty()) tokens.push_back(move(token));
return tokens;
}
/**
* @param (str) String
* @param (delim) String
* @brief Join a string.
*/
static inline string join(const vector<string>& str, const string& delim) {
string result;
for (size_t i = 0; i < str.size(); ++i) {
result += str[i];
if (i != str.size() - 1) {
result += delim;
}
}
return result;
}
/**
* @param (str) String
* @return True if null or empty, else false
*/
inline bool isNullorEmpty() const
{
return isNullorEmpty(vstr);
}
/**
* @param (str) String
* @return True if null or Whitespace, else false
*/
inline bool isNullorWhiteSpace() const
{
return isNullorWhiteSpace(vstr);
}
/**
* @param (prefix) Prefix of the string to be searched
* @return True if the string starts at the prefix value, else false
*/
inline bool startsWith(const string prefix) const
{
return startsWith(vstr, prefix);
}
/**
* @param (pattern) Regular expression to adapt to the string to be searched
* @return True if the string starts at the prefix value, else false
*/
inline bool startsWith(const regex& pattern) const
{
return startsWith(vstr, pattern);
}
/**
* @param (suffix) Suffix of the string to be searched
* @return True if the string starts at the suffix value, else false
*/
inline bool endsWith(const string suffix) const
{
return endsWith(vstr, suffix);
}
/**
* @param (pattern) Regular expression to adapt to the string to be searched
* @return True if the string starts at the pattern regex, else false
*/
inline bool endsWith(const regex& pattern) const
{
return endsWith(vstr, pattern);
}
/**
* @param (search) Char
* @return Returns the position of the character to be searched for as a number. Returns -1 if not found.
*/
inline int indexOf(char search) const
{
return indexOf(vstr, search);
}
/**
* @param (delim) String
* @brief Splits a string.
*/
inline vector<string> split(char delim) const
{
return split(vstr, delim);
}
StringEx& operator=(const char* str) {
string::operator=(str);
vstr = str;
return *this;
}
StringEx& operator=(const string& str) {
string::operator=(str);
vstr = str;
return *this;
}
};
}
typedef StringEx::StringEx stringEx;
#endif // !STRING_EX_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment