diff --git a/Jupiter/String_Type.h b/Jupiter/String_Type.h index 52c1708..1ad5ba6 100644 --- a/Jupiter/String_Type.h +++ b/Jupiter/String_Type.h @@ -18,35 +18,60 @@ #if !defined _STRING_TYPE_H_HEADER #define _STRING_TYPE_H_HEADER -#include "Jupiter.h" -#include // std::basic_string type - /** * @file String_Type.h * @brief Provides the basis for String types, of any implementation. * Note: Some methods are commented out. This means that they should be implemented, but could not be put declared in this template (return of abstract type). */ +#include "Jupiter.h" +#include // std::basic_string type +#include // va_list +#include +#include + 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 class String_Type { public: /** - * @brief Returns a C-Style string version of the String. + * @brief Fetches an element from the string. * - * @return C-Style string representation of the String. + * @param index Index of the element to return. + * @return The element located at the specified index. */ - virtual const T *c_str() const = 0; + T &get(size_t index) const; /** * @brief Returns the number of elements in the String. * * @return Number of elements in the string. */ - virtual size_t size() const = 0; + size_t size() const; + + /** + * @brief Returns a pointer to the underlying string of elements. + * + * @return Pointer to the underlying string of elements. + */ + const T *ptr() const; + + /** + * @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. @@ -54,10 +79,11 @@ namespace Jupiter * @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. */ - virtual int compare(const String_Type &in) const = 0; - virtual int compare(const std::basic_string &in) const = 0; - virtual int compare(const T *in) const = 0; - virtual int compare(const T in) const = 0; + int compare(const String_Type &in) const; + int compare(const std::basic_string &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. @@ -66,10 +92,11 @@ namespace Jupiter * @param in String to compare against. * @return True if the contents of the strings are equal, false otherwise. */ - virtual bool equals(const String_Type &in) const = 0; - virtual bool equals(const std::basic_string &in) const = 0; - virtual bool equals(const T *in) const = 0; - virtual bool equals(const T in) const = 0; + bool equals(const String_Type &in) const; + bool equals(const std::basic_string &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. @@ -78,10 +105,11 @@ namespace Jupiter * @param in String to compare against. * @return True if the contents of the strings are equal, false otherwise. */ - virtual bool equalsi(const String_Type &in) const = 0; - virtual bool equalsi(const std::basic_string &in) const = 0; - virtual bool equalsi(const T *in) const = 0; - virtual bool equalsi(const T in) const = 0; + bool equalsi(const String_Type &in) const; + bool equalsi(const std::basic_string &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. @@ -90,9 +118,9 @@ namespace Jupiter * @param format Format that the string is compared against. * @return True if the String matches the wildcard format, false otherwise. */ - virtual bool match(const String_Type &format) const = 0; - virtual bool match(const std::basic_string &format) const = 0; - virtual bool match(const T *format) const = 0; + bool match(const String_Type &format) const; + bool match(const std::basic_string &format) const; + bool match(const T *format) const; /** * @brief Checks if the CString matches a wildcard format. @@ -101,62 +129,86 @@ namespace Jupiter * @param format Format that the string is compared against. * @return True if the CString matches the wildcard format, false otherwise. */ - virtual bool matchi(const String_Type &format) const = 0; - virtual bool matchi(const std::basic_string &format) const = 0; - virtual bool matchi(const T *format) const = 0; + bool matchi(const String_Type &format) const; + bool matchi(const std::basic_string &format) const; + bool matchi(const T *format) const; /** - * @brief Sets the CString's contents based on the format string and input variables. - * Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t. + * @brief Counts the number of token deliminated words. * - * @param format Format that the string is compared against. - * @param ... Inputs to match the format specifiers. - * @return Number of characters written. + * @param whitespace A string of tokens used to deliminate words. + * @return Number of words found. */ - virtual size_t format(const String_Type &format, ...) = 0; - virtual size_t format(const std::basic_string &format, ...) = 0; - virtual size_t format(const T *format, ...) = 0; - virtual size_t vformat(const T *format, va_list args) = 0; + unsigned int wordCount(const T *whitespace) const; /** - * @brief Appends to a CString's contents based on the format string and input variables. - * Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t. + * @brief Interprets the string as an integer. + * Note: This returns 0 on any value string type other than char. * - * @param format Format that the string is compared against. - * @param ... Inputs to match the format specifiers. - * @return Number of characters written. + * @param base Base of the string representation. + * @return Integer representation of the string. */ - virtual size_t aformat(const String_Type &format, ...) = 0; - virtual size_t aformat(const std::basic_string &format, ...) = 0; - virtual size_t aformat(const T *format, ...) = 0; - virtual size_t avformat(const T *format, va_list args) = 0; + int asInt(int base = 0) const; /** - * @brief Counts the number of token deliminated words. + * @brief Interprets the string as an integer. + * Note: This returns 0 on any value string type other than char. * - * @param whitespace A string of tokens used to deliminate words. - * @return Number of words found. + * @param base Base of the string representation. + * @return Integer representation of the string. */ - virtual unsigned int wordCount(const T *whitespace) const = 0; + unsigned int asUnsignedInt(int base = 0) const; /** - * @brief Creates a partial copy of the string. + * @brief Returns a C-Style string version of the String. * - * @param pos Position in the string to start copying from. - * @param length Number of characters to copy. - * @return String containing a partial copy of the original string. + * @return C-Style string representation of the String. */ - //virtual String_Type substring(size_t pos, size_t length) const = 0; - //String_Type substr(size_t pos, size_t length) { return this->substring(pos, length); } + virtual const T *c_str() const = 0; /** - * @brief Creates a partial copy of the string, based on a set of tokens. + * @brief Outputs the string to a FILE stream. * - * @param pos Position in the string to start copying from. - * @param whitespace A string of tokens used to deliminate words. - * @return String containing a partial copy of the original string. + * @param out Stream to output to. + * @return Number of elements written successfully. */ - //virtual String_Type getWord(size_t pos, T *whitespace) const = 0; + size_t print(FILE *out) const; + size_t print(std::basic_ostream &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 &out) const; + + /** + * @brief Sets the CString's contents based on the format string and input variables. + * Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t. + * + * @param format Format that the string is compared against. + * @param ... Inputs to match the format specifiers. + * @return Number of characters written. + */ + size_t format(const String_Type &format, ...); + size_t format(const std::basic_string &format, ...); + size_t format(const T *format, ...); + virtual size_t vformat(const T *format, va_list args) = 0; + + /** + * @brief Appends to a CString's contents based on the format string and input variables. + * Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t. + * + * @param format Format that the string is compared against. + * @param ... Inputs to match the format specifiers. + * @return Number of characters written. + */ + size_t aformat(const String_Type &format, ...); + size_t aformat(const std::basic_string &format, ...); + size_t aformat(const T *format, ...); + virtual size_t avformat(const T *format, va_list args) = 0; /** * @brief Copies the data from the input string to the String. @@ -188,16 +240,8 @@ namespace Jupiter */ virtual size_t truncate(size_t n) = 0; - /** - * @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; - /** Access operator */ - inline T &operator[](size_t index) { return this->get(index); }; + inline T &operator[](size_t index) { return Jupiter::String_Type::get(index); }; // Mutative operators. // Note: All extending classes must overload operator= for its own type. @@ -248,6 +292,7 @@ namespace Jupiter protected: T *str; /** Pointer for the underlying string of elements */ + size_t length; /** Number of representable elements in the string */ }; /** Generic String Type */ @@ -258,4 +303,7 @@ namespace Jupiter } +/** Implementation for String_Type. */ +#include "String_Type_Imp.h" + #endif // _STRING_TYPE_H_HEADER \ No newline at end of file