mirror of https://github.com/JAJames/Jupiter.git
JustinAJ
11 years ago
7 changed files with 937 additions and 826 deletions
@ -0,0 +1,181 @@ |
|||
/**
|
|||
* Copyright (C) 2014 Justin James. |
|||
* |
|||
* This license must be preserved. |
|||
* Any applications, libraries, or code which make any use of any |
|||
* component of this program must not be commercial, unless explicit |
|||
* permission is granted from the original author. The use of this |
|||
* program for non-profit purposes is permitted. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* |
|||
* In the event that this license restricts you from making desired use of this program, contact the original author. |
|||
* Written by Justin James <justin.aj@hotmail.com> |
|||
*/ |
|||
|
|||
#if !defined _READABLE_STRING_H_HEADER |
|||
#define _READABLE_STRING_H_HEADER |
|||
|
|||
/**
|
|||
* @file Readable_String.h |
|||
* @brief Defines several basic accessive and comparative virtual functions for strings. |
|||
*/ |
|||
|
|||
#include <cwchar> // wchar_t |
|||
#include <cstdio> // FILE |
|||
#include <string> // std::basic_string<T> type |
|||
|
|||
namespace Jupiter |
|||
{ |
|||
|
|||
/**
|
|||
* @brief Provides the basis for String classes by providing implementations for operators, comparative operations, and defining abstract functions. |
|||
* Note: This is an abstract type. |
|||
* |
|||
* @param T Element type which the String will store. Defaults to char. |
|||
*/ |
|||
template<typename T = char> class Readable_String |
|||
{ |
|||
public: |
|||
|
|||
/**
|
|||
* @brief Fetches an element from the string. |
|||
* |
|||
* @param index Index of the element to return. |
|||
* @return The element located at the specified index. |
|||
*/ |
|||
virtual T &get(size_t index) const = 0; |
|||
|
|||
/**
|
|||
* @brief Returns the number of elements in the String. |
|||
* |
|||
* @return Number of elements in the string. |
|||
*/ |
|||
virtual size_t size() const = 0; |
|||
|
|||
/**
|
|||
* @brief Returns a pointer to the underlying string of elements. |
|||
* |
|||
* @return Pointer to the underlying string of elements. |
|||
*/ |
|||
virtual const T *ptr() const = 0; |
|||
|
|||
/**
|
|||
* @brief Checks if the string contains an element with the specified value. |
|||
* |
|||
* @param value Value of the element to search for. |
|||
* @return True if a match is found, false otherwise. |
|||
*/ |
|||
bool contains(const T &value) const; |
|||
|
|||
/**
|
|||
* @brief Compares another string against the String. |
|||
* |
|||
* @param in String to compare against. |
|||
* @return 0 if the strings are equal, negative if the first mismatched character is greater in the String, or positive if it's less. |
|||
*/ |
|||
int compare(const Readable_String<T> &in) const; |
|||
int compare(const std::basic_string<T> &in) const; |
|||
int compare(const T *in) const; |
|||
int compare(const T &in) const; |
|||
int compare(const std::nullptr_t) const; |
|||
|
|||
/**
|
|||
* @brief Checks if the strings are equal. |
|||
* Note: Case sensitive. |
|||
* |
|||
* @param in String to compare against. |
|||
* @return True if the contents of the strings are equal, false otherwise. |
|||
*/ |
|||
bool equals(const Readable_String<T> &in) const; |
|||
bool equals(const std::basic_string<T> &in) const; |
|||
bool equals(const T *in) const; |
|||
bool equals(const T &in) const; |
|||
bool equals(const std::nullptr_t) const; |
|||
|
|||
/**
|
|||
* @brief Checks if the strings are equal. |
|||
* Note: Case insensitive. Returns false for any type other than char and wchar_t. |
|||
* |
|||
* @param in String to compare against. |
|||
* @return True if the contents of the strings are equal, false otherwise. |
|||
*/ |
|||
bool equalsi(const Readable_String<T> &in) const; |
|||
bool equalsi(const std::basic_string<T> &in) const; |
|||
bool equalsi(const T *in) const; |
|||
bool equalsi(const T &in) const; |
|||
bool equalsi(const std::nullptr_t) const; |
|||
|
|||
/**
|
|||
* @brief Checks if the String matches a wildcard format. |
|||
* Note: Case sensitive. |
|||
* |
|||
* @param format Format that the string is compared against. |
|||
* @return True if the String matches the wildcard format, false otherwise. |
|||
*/ |
|||
bool match(const Readable_String<T> &format) const; |
|||
bool match(const std::basic_string<T> &format) const; |
|||
bool match(const T *format) const; |
|||
|
|||
/**
|
|||
* @brief Checks if the CString matches a wildcard format. |
|||
* Note: Case insensitive. Returns false for any type other than char and wchar_t. |
|||
* |
|||
* @param format Format that the string is compared against. |
|||
* @return True if the CString matches the wildcard format, false otherwise. |
|||
*/ |
|||
bool matchi(const Readable_String<T> &format) const; |
|||
bool matchi(const std::basic_string<T> &format) const; |
|||
bool matchi(const T *format) const; |
|||
|
|||
/**
|
|||
* @brief Counts the number of token deliminated words. |
|||
* |
|||
* @param whitespace A string of tokens used to deliminate words. |
|||
* @return Number of words found. |
|||
*/ |
|||
unsigned int wordCount(const T *whitespace) const; |
|||
|
|||
/**
|
|||
* @brief Interprets the string as an integer. |
|||
* Note: This returns 0 on any value string type other than char. |
|||
* |
|||
* @param base Base of the string representation. |
|||
* @return Integer representation of the string. |
|||
*/ |
|||
int asInt(int base = 0) const; |
|||
|
|||
/**
|
|||
* @brief Interprets the string as an integer. |
|||
* Note: This returns 0 on any value string type other than char. |
|||
* |
|||
* @param base Base of the string representation. |
|||
* @return Integer representation of the string. |
|||
*/ |
|||
unsigned int asUnsignedInt(int base = 0) const; |
|||
|
|||
/**
|
|||
* @brief Outputs the string to a FILE stream. |
|||
* |
|||
* @param out Stream to output to. |
|||
* @return Number of elements written successfully. |
|||
*/ |
|||
size_t print(FILE *out) const; |
|||
size_t print(std::basic_ostream<T> &out) const; |
|||
|
|||
/**
|
|||
* @brief Outputs the string and a newline to a FILE stream |
|||
* |
|||
* @param out Stream to output to. |
|||
* @param Number of elements written successfully. |
|||
*/ |
|||
size_t println(FILE *out) const; |
|||
size_t println(std::basic_ostream<T> &out) const; |
|||
}; |
|||
} |
|||
|
|||
#include "Readable_String_Imp.h" |
|||
|
|||
#endif // _READABLE_STRING_H_HEADER
|
@ -0,0 +1,746 @@ |
|||
/**
|
|||
* Copyright (C) 2014 Justin James. |
|||
* |
|||
* This license must be preserved. |
|||
* Any applications, libraries, or code which make any use of any |
|||
* component of this program must not be commercial, unless explicit |
|||
* permission is granted from the original author. The use of this |
|||
* program for non-profit purposes is permitted. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* |
|||
* In the event that this license restricts you from making desired use of this program, contact the original author. |
|||
* Written by Justin James <justin.aj@hotmail.com> |
|||
*/ |
|||
|
|||
#if !defined _READABLE_STRING_IMP_H_HEADER |
|||
#define _READABLE_STRING_IMP_H_HEADER |
|||
|
|||
/**
|
|||
* @file Readable_String_Imp.h |
|||
* @brief Provides the implementations for Readable_String functions. |
|||
* Note: Modification of this file is not supported in any way. |
|||
*/ |
|||
|
|||
#include "Readable_String.h" |
|||
#include "Functions.h" |
|||
|
|||
/**
|
|||
* IMPLEMENTATION: |
|||
* Readable_String |
|||
*/ |
|||
|
|||
// contains
|
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::contains(const T &value) const |
|||
{ |
|||
for (size_t i = 0; i != this->size(); i++) if (this->get(i) == value) return true; |
|||
return false; |
|||
} |
|||
|
|||
// compare()
|
|||
|
|||
template<typename T> int Jupiter::Readable_String<T>::compare(const Jupiter::Readable_String<T> &in) const |
|||
{ |
|||
// rewrite to compare multiple bytes at a time.
|
|||
size_t index = 0; |
|||
while (this->get(index) == in.get(index)) |
|||
{ |
|||
index++; |
|||
if (index == in.size()) |
|||
{ |
|||
if (index == this->size()) return 0; |
|||
return 1; |
|||
} |
|||
if (index == this->size()) return 0 - in.get(index); |
|||
} |
|||
return this->get(index) - in.get(index); |
|||
} |
|||
|
|||
template<typename T> int Jupiter::Readable_String<T>::compare(const std::basic_string<T> &in) const |
|||
{ |
|||
// rewrite to compare multiple bytes at a time.
|
|||
size_t index = 0; |
|||
while (this->get(index) == in.at(index)) |
|||
{ |
|||
index++; |
|||
if (index == in.size()) |
|||
{ |
|||
if (index == this->size()) return 0; |
|||
return 1; |
|||
} |
|||
if (index == this->size()) return 0 - in.at(index); |
|||
} |
|||
return this->get(index) - in.at(index); |
|||
} |
|||
|
|||
template<typename T> int Jupiter::Readable_String<T>::compare(const T *s2) const |
|||
{ |
|||
if (this->size() == 0) return 0 - *s2; |
|||
size_t index = 0; |
|||
while (this->get(index) == *s2) |
|||
{ |
|||
index++; |
|||
if (*s2 == 0) |
|||
{ |
|||
if (index == this->size()) return 0; |
|||
return 1; |
|||
} |
|||
s2++; |
|||
if (index == this->size()) return 0 - *s2; |
|||
} |
|||
return this->get(index) - *s2; |
|||
} |
|||
|
|||
template<typename T> int Jupiter::Readable_String<T>::compare(const T &in) const |
|||
{ |
|||
if (this->size() == 0) return 0 - in; |
|||
return this->get(0) - in; |
|||
} |
|||
|
|||
template<typename T> int Jupiter::Readable_String<T>::compare(const std::nullptr_t) const |
|||
{ |
|||
if (this->size() == 0) return true; |
|||
return this->get(0); |
|||
} |
|||
|
|||
// equals()
|
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equals(const Jupiter::Readable_String<T> &in) const |
|||
{ |
|||
if (this->size() != in.size()) return false; |
|||
|
|||
// rewrite to compare multiple bytes at a time.
|
|||
size_t index = 0; |
|||
while (index != this->size()) |
|||
{ |
|||
if (this->get(index) != in.get(index)) return false; |
|||
index++; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equals(const std::basic_string<T> &in) const |
|||
{ |
|||
if (this->size() != in.size()) return false; |
|||
// rewrite to compare multiple bytes at a time.
|
|||
size_t index = 0; |
|||
while (index != this->size()) |
|||
{ |
|||
if (this->get(index) != in.at(index)) return false; |
|||
index++; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equals(const T *in) const |
|||
{ |
|||
if (in == nullptr) return this->size() == 0; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (*in == 0 || this->get(index) != *in) return false; |
|||
in++; |
|||
} |
|||
return *in == 0; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equals(const T &in) const |
|||
{ |
|||
return this->size() == 1 && this->get(0) == in; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equals(std::nullptr_t) const |
|||
{ |
|||
return this->size() == 0; |
|||
} |
|||
|
|||
// equalsi()
|
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::equalsi(const Jupiter::Readable_String<char> &in) const |
|||
{ |
|||
if (this->size() != in.size()) return false; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (toupper(this->get(index)) != toupper(in.get(index))) return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::equalsi(const Jupiter::Readable_String<wchar_t> &in) const |
|||
{ |
|||
if (this->size() != in.size()) return false; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (towupper(this->get(index)) != towupper(in.get(index))) return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equalsi(const Jupiter::Readable_String<T> &in) const |
|||
{ |
|||
return this->equals(in); // Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::equalsi(const std::basic_string<char> &in) const |
|||
{ |
|||
if (this->size() != in.size()) return false; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (toupper(this->get(index)) != toupper(in.at(index))) return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::equalsi(const std::basic_string<wchar_t> &in) const |
|||
{ |
|||
if (this->size() != in.size()) return false; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (towupper(this->get(index)) != towupper(in.at(index))) return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equalsi(const std::basic_string<T> &in) const |
|||
{ |
|||
return this->equals(in); // Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::equalsi(const char *in) const |
|||
{ |
|||
if (in == nullptr) return this->size() == 0; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (*in == 0 || toupper(this->get(index)) != toupper(*in)) return false; |
|||
in++; |
|||
} |
|||
return *in == 0; |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::equalsi(const wchar_t *in) const |
|||
{ |
|||
if (in == nullptr) return this->size() == 0; |
|||
for (size_t index = 0; index != this->size(); index++) |
|||
{ |
|||
if (*in == 0 || towupper(this->get(index)) != towupper(*in)) return false; |
|||
in++; |
|||
} |
|||
return *in == 0; |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equalsi(const T *in) const |
|||
{ |
|||
return this->equals(in); // Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::equalsi(const char &in) const |
|||
{ |
|||
return this->size() == 1 && toupper(this->get(0)) == toupper(in); |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::equalsi(const wchar_t &in) const |
|||
{ |
|||
return this->size() == 1 && towupper(this->get(0)) == towupper(in); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equalsi(const T &in) const |
|||
{ |
|||
return this->equals(in); // Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::equalsi(const std::nullptr_t) const |
|||
{ |
|||
return this->size() == 0; |
|||
} |
|||
|
|||
// match()
|
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::match(const Jupiter::Readable_String<char> &format) const |
|||
{ |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.get(formatIndex) == '*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.get(formatIndex) == '?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.get(formatIndex) == 0) return true; |
|||
if (format.get(formatIndex) == '*') continue; |
|||
while (format.get(formatIndex) != this->get(index)) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.get(formatIndex) != '?' && format.get(formatIndex) != this->get(index)) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::match(const Jupiter::Readable_String<wchar_t> &format) const |
|||
{ |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.get(formatIndex) == '*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.get(formatIndex) == '?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.get(formatIndex) == 0) return true; |
|||
if (format.get(formatIndex) == '*') continue; |
|||
while (format.get(formatIndex) != this->get(index)) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.get(formatIndex) != '?' && format.get(formatIndex) != this->get(index)) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::match(const Jupiter::Readable_String<T> &format) const |
|||
{ |
|||
return false; // Wildcard matching not supported for type.
|
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::match(const std::basic_string<char> &format) const |
|||
{ |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.at(formatIndex) == '*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.at(formatIndex) == '?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.at(formatIndex) == 0) return true; |
|||
if (format.at(formatIndex) == '*') continue; |
|||
while (format.at(formatIndex) != this->get(index)) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.at(formatIndex) != '?' && format.at(formatIndex) != this->get(index)) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::match(const std::basic_string<wchar_t> &format) const |
|||
{ |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.at(formatIndex) == '*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.at(formatIndex) == '?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.at(formatIndex) == 0) return true; |
|||
if (format.at(formatIndex) == '*') continue; |
|||
while (format.at(formatIndex) != this->get(index)) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.at(formatIndex) != '?' && format.at(formatIndex) != this->get(index)) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::match(const std::basic_string<T> &format) const |
|||
{ |
|||
return false; // Wildcard matching not supported for type.
|
|||
} |
|||
|
|||
template<> inline bool Jupiter::Readable_String<char>::match(const char *format) const |
|||
{ |
|||
size_t index = 0; |
|||
while (*format != 0) |
|||
{ |
|||
if (*format == '*') |
|||
{ |
|||
format++; |
|||
while (*format == '?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
if (*format == 0) return true; |
|||
if (*format == '*') continue; |
|||
while (*format != this->get(index)) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (*format != '?' && *format != this->get(index)) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<> inline bool Jupiter::Readable_String<wchar_t>::match(const wchar_t *format) const |
|||
{ |
|||
size_t index = 0; |
|||
while (*format != 0) |
|||
{ |
|||
if (*format == L'*') |
|||
{ |
|||
format++; |
|||
while (*format == L'?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
if (*format == 0) return true; |
|||
if (*format == L'*') continue; |
|||
while (*format != this->get(index)) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (*format != L'?' && *format != this->get(index)) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::match(const T *format) const |
|||
{ |
|||
return false; // Wildcard matching not supported for type.
|
|||
} |
|||
|
|||
// matchi()
|
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::matchi(const Jupiter::Readable_String<char> &format) const |
|||
{ |
|||
int fUpper; |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.get(formatIndex) == L'*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.get(formatIndex) == L'?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.get(formatIndex) == 0) return true; |
|||
if (format.get(formatIndex) == '*') continue; |
|||
fUpper = toupper(format.get(formatIndex)); |
|||
while (fUpper != toupper(this->get(index))) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.get(formatIndex) != L'?' && toupper(format.get(formatIndex)) != toupper(this->get(index))) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::matchi(const Jupiter::Readable_String<wchar_t> &format) const |
|||
{ |
|||
wint_t fUpper; |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.get(formatIndex) == L'*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.get(formatIndex) == L'?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.get(formatIndex) == 0) return true; |
|||
if (format.get(formatIndex) == '*') continue; |
|||
fUpper = towupper(format.get(formatIndex)); |
|||
while (fUpper != towupper(this->get(index))) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.get(formatIndex) != L'?' && towupper(format.get(formatIndex)) != towupper(this->get(index))) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::matchi(const Jupiter::Readable_String<T> &format) const |
|||
{ |
|||
return false; // Wildcard matching not supported for type. Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::matchi(const std::basic_string<char> &format) const |
|||
{ |
|||
int fUpper; |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.at(formatIndex) == L'*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.at(formatIndex) == L'?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.at(formatIndex) == 0) return true; |
|||
if (format.at(formatIndex) == '*') continue; |
|||
fUpper = toupper(format.at(formatIndex)); |
|||
while (fUpper != toupper(this->get(index))) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.at(formatIndex) != L'?' && toupper(format.at(formatIndex)) != toupper(this->get(index))) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::matchi(const std::basic_string<wchar_t> &format) const |
|||
{ |
|||
wint_t fUpper; |
|||
size_t index = 0; |
|||
size_t formatIndex = 0; |
|||
while (formatIndex != format.size()) |
|||
{ |
|||
if (format.at(formatIndex) == L'*') |
|||
{ |
|||
formatIndex++; |
|||
while (format.at(formatIndex) == L'?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
if (format.at(formatIndex) == 0) return true; |
|||
if (format.at(formatIndex) == '*') continue; |
|||
fUpper = towupper(format.at(formatIndex)); |
|||
while (fUpper != towupper(this->get(index))) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (format.at(formatIndex) != L'?' && towupper(format.at(formatIndex)) != towupper(this->get(index))) return false; |
|||
formatIndex++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::matchi(const std::basic_string<T> &format) const |
|||
{ |
|||
return false; // Wildcard matching not supported for type. Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<char>::matchi(const char *format) const |
|||
{ |
|||
int fUpper; |
|||
size_t index = 0; |
|||
while (*format != 0) |
|||
{ |
|||
if (*format == '*') |
|||
{ |
|||
format++; |
|||
while (*format == '?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
if (*format == 0) return true; |
|||
if (*format == '*') continue; |
|||
fUpper = toupper(*format); |
|||
while (fUpper != toupper(this->get(index))) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (*format != '?' && toupper(*format) != toupper(this->get(index))) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<> bool inline Jupiter::Readable_String<wchar_t>::matchi(const wchar_t *format) const |
|||
{ |
|||
wint_t fUpper; |
|||
size_t index = 0; |
|||
while (*format != 0) |
|||
{ |
|||
if (*format == L'*') |
|||
{ |
|||
format++; |
|||
while (*format == L'?') |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
if (*format == 0) return true; |
|||
if (*format == '*') continue; |
|||
fUpper = towupper(*format); |
|||
while (fUpper != towupper(this->get(index))) |
|||
{ |
|||
if (this->get(index) == 0) return false; |
|||
index++; |
|||
} |
|||
} |
|||
else if (*format != L'?' && towupper(*format) != towupper(this->get(index))) return false; |
|||
format++; |
|||
index++; |
|||
} |
|||
return index == this->size(); |
|||
} |
|||
|
|||
template<typename T> bool Jupiter::Readable_String<T>::matchi(const T *format) const |
|||
{ |
|||
return false; // Wildcard matching not supported for type. Concept of "case" not supported for type.
|
|||
} |
|||
|
|||
// wordCount()
|
|||
|
|||
template<typename T> unsigned int Jupiter::Readable_String<T>::wordCount(const T *whitespace) const |
|||
{ |
|||
unsigned int result = 0; |
|||
const T *p = this->ptr(); |
|||
bool prev = true; |
|||
while (*p != 0) |
|||
{ |
|||
if (Jupiter::strpbrk<T>(whitespace, *p) == nullptr) // This isn't whitespace!
|
|||
{ |
|||
if (prev == true) // We just left whitespace!
|
|||
{ |
|||
prev = false; |
|||
result++; |
|||
} |
|||
} |
|||
else prev = true; // This is whitespace!
|
|||
p++; |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
// as<type>
|
|||
|
|||
template<> unsigned int inline Jupiter::Readable_String<char>::asUnsignedInt(int base) const |
|||
{ |
|||
return strtoui_s(this->ptr(), this->size(), base); |
|||
} |
|||
|
|||
template<typename T> unsigned int Jupiter::Readable_String<T>::asUnsignedInt(int base) const |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
template<> int inline Jupiter::Readable_String<char>::asInt(int base) const |
|||
{ |
|||
return strtoi_s(this->ptr(), this->size(), base); |
|||
} |
|||
|
|||
template<typename T> int Jupiter::Readable_String<T>::asInt(int base) const |
|||
{ |
|||
return 0; |
|||
} |
|||
|
|||
// Stream output
|
|||
|
|||
template<typename T> size_t Jupiter::Readable_String<T>::print(FILE *out) const |
|||
{ |
|||
return fwrite(this->ptr(), sizeof(T), this->size(), out); |
|||
} |
|||
|
|||
template<> size_t inline Jupiter::Readable_String<wchar_t>::print(FILE *out) const |
|||
{ |
|||
size_t index = 0; |
|||
while (index != this->size() && fputwc(this->get(index), out) != WEOF) index++; |
|||
return index; |
|||
} |
|||
|
|||
template<typename T> size_t Jupiter::Readable_String<T>::print(std::basic_ostream<T> &out) const |
|||
{ |
|||
size_t index = 0; |
|||
while (index != this->size()) |
|||
{ |
|||
out << this->get(index); |
|||
index++; |
|||
} |
|||
return index; |
|||
} |
|||
|
|||
template<typename T> size_t Jupiter::Readable_String<T>::println(FILE *out) const |
|||
{ |
|||
size_t r = this->print(out); |
|||
if (r != this->size()) return r; |
|||
if (fputs("\r\n", out) != EOF) r += 2; |
|||
return r; |
|||
} |
|||
|
|||
template<typename T> size_t Jupiter::Readable_String<T>::println(std::basic_ostream<T> &out) const |
|||
{ |
|||
size_t r = this->print(out); |
|||
if (r != this->size()) return r; |
|||
out << std::endl; |
|||
return r; |
|||
} |
|||
|
|||
#endif // _READABLE_STRING_IMP_H_HEADER
|
Binary file not shown.
Loading…
Reference in new issue