Browse Source

Moved size variable and many comparative operations to String_Type.

release/0.19
JustinAJ 11 years ago
parent
commit
2f2c7e8056
  1. 182
      Jupiter/String_Type.h

182
Jupiter/String_Type.h

@ -18,35 +18,60 @@
#if !defined _STRING_TYPE_H_HEADER #if !defined _STRING_TYPE_H_HEADER
#define _STRING_TYPE_H_HEADER #define _STRING_TYPE_H_HEADER
#include "Jupiter.h"
#include <string> // std::basic_string<T> type
/** /**
* @file String_Type.h * @file String_Type.h
* @brief Provides the basis for String types, of any implementation. * @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). * 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 <string> // std::basic_string<T> type
#include <cstdarg> // va_list
#include <cwchar>
#include <cstdio>
namespace Jupiter 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 String_Type template<typename T = char> class String_Type
{ {
public: 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. * @brief Returns the number of elements in the String.
* *
* @return 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. * @brief Compares another string against the String.
@ -54,10 +79,11 @@ namespace Jupiter
* @param in String to compare against. * @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. * @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<T> &in) const = 0; int compare(const String_Type<T> &in) const;
virtual int compare(const std::basic_string<T> &in) const = 0; int compare(const std::basic_string<T> &in) const;
virtual int compare(const T *in) const = 0; int compare(const T *in) const;
virtual int compare(const T in) const = 0; int compare(const T &in) const;
int compare(const std::nullptr_t) const;
/** /**
* @brief Checks if the strings are equal. * @brief Checks if the strings are equal.
@ -66,10 +92,11 @@ namespace Jupiter
* @param in String to compare against. * @param in String to compare against.
* @return True if the contents of the strings are equal, false otherwise. * @return True if the contents of the strings are equal, false otherwise.
*/ */
virtual bool equals(const String_Type<T> &in) const = 0; bool equals(const String_Type<T> &in) const;
virtual bool equals(const std::basic_string<T> &in) const = 0; bool equals(const std::basic_string<T> &in) const;
virtual bool equals(const T *in) const = 0; bool equals(const T *in) const;
virtual bool equals(const T in) const = 0; bool equals(const T &in) const;
bool equals(const std::nullptr_t) const;
/** /**
* @brief Checks if the strings are equal. * @brief Checks if the strings are equal.
@ -78,10 +105,11 @@ namespace Jupiter
* @param in String to compare against. * @param in String to compare against.
* @return True if the contents of the strings are equal, false otherwise. * @return True if the contents of the strings are equal, false otherwise.
*/ */
virtual bool equalsi(const String_Type<T> &in) const = 0; bool equalsi(const String_Type<T> &in) const;
virtual bool equalsi(const std::basic_string<T> &in) const = 0; bool equalsi(const std::basic_string<T> &in) const;
virtual bool equalsi(const T *in) const = 0; bool equalsi(const T *in) const;
virtual bool equalsi(const T in) const = 0; bool equalsi(const T &in) const;
bool equalsi(const std::nullptr_t) const;
/** /**
* @brief Checks if the String matches a wildcard format. * @brief Checks if the String matches a wildcard format.
@ -90,9 +118,9 @@ namespace Jupiter
* @param format Format that the string is compared against. * @param format Format that the string is compared against.
* @return True if the String matches the wildcard format, false otherwise. * @return True if the String matches the wildcard format, false otherwise.
*/ */
virtual bool match(const String_Type<T> &format) const = 0; bool match(const String_Type<T> &format) const;
virtual bool match(const std::basic_string<T> &format) const = 0; bool match(const std::basic_string<T> &format) const;
virtual bool match(const T *format) const = 0; bool match(const T *format) const;
/** /**
* @brief Checks if the CString matches a wildcard format. * @brief Checks if the CString matches a wildcard format.
@ -101,62 +129,86 @@ namespace Jupiter
* @param format Format that the string is compared against. * @param format Format that the string is compared against.
* @return True if the CString matches the wildcard format, false otherwise. * @return True if the CString matches the wildcard format, false otherwise.
*/ */
virtual bool matchi(const String_Type<T> &format) const = 0; bool matchi(const String_Type<T> &format) const;
virtual bool matchi(const std::basic_string<T> &format) const = 0; bool matchi(const std::basic_string<T> &format) const;
virtual bool matchi(const T *format) const = 0; bool matchi(const T *format) const;
/** /**
* @brief Sets the CString's contents based on the format string and input variables. * @brief Counts the number of token deliminated words.
* 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 whitespace A string of tokens used to deliminate words.
* @param ... Inputs to match the format specifiers. * @return Number of words found.
* @return Number of characters written.
*/ */
virtual size_t format(const String_Type<T> &format, ...) = 0; unsigned int wordCount(const T *whitespace) const;
virtual size_t format(const std::basic_string<T> &format, ...) = 0;
virtual size_t format(const T *format, ...) = 0;
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. * @brief Interprets the string as an integer.
* Note: Format specifiers similar to printf. Returns 0 for any type other than char and wchar_t. * Note: This returns 0 on any value string type other than char.
* *
* @param format Format that the string is compared against. * @param base Base of the string representation.
* @param ... Inputs to match the format specifiers. * @return Integer representation of the string.
* @return Number of characters written.
*/ */
virtual size_t aformat(const String_Type<T> &format, ...) = 0; int asInt(int base = 0) const;
virtual size_t aformat(const std::basic_string<T> &format, ...) = 0;
virtual size_t aformat(const T *format, ...) = 0;
virtual size_t avformat(const T *format, va_list args) = 0;
/** /**
* @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. * @param base Base of the string representation.
* @return Number of words found. * @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. * @return C-Style string representation of the String.
* @param length Number of characters to copy.
* @return String containing a partial copy of the original string.
*/ */
//virtual String_Type<T> substring(size_t pos, size_t length) const = 0; virtual const T *c_str() const = 0;
//String_Type<T> substr(size_t pos, size_t length) { return this->substring(pos, length); }
/** /**
* @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 out Stream to output to.
* @param whitespace A string of tokens used to deliminate words. * @return Number of elements written successfully.
* @return String containing a partial copy of the original string.
*/ */
//virtual String_Type<T> getWord(size_t pos, T *whitespace) const = 0; 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;
/**
* @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<T> &format, ...);
size_t format(const std::basic_string<T> &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<T> &format, ...);
size_t aformat(const std::basic_string<T> &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. * @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; 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 */ /** Access operator */
inline T &operator[](size_t index) { return this->get(index); }; inline T &operator[](size_t index) { return Jupiter::String_Type<T>::get(index); };
// Mutative operators. // Mutative operators.
// Note: All extending classes must overload operator= for its own type. // Note: All extending classes must overload operator= for its own type.
@ -248,6 +292,7 @@ namespace Jupiter
protected: protected:
T *str; /** Pointer for the underlying string of elements */ T *str; /** Pointer for the underlying string of elements */
size_t length; /** Number of representable elements in the string */
}; };
/** Generic String Type */ /** Generic String Type */
@ -258,4 +303,7 @@ namespace Jupiter
} }
/** Implementation for String_Type. */
#include "String_Type_Imp.h"
#endif // _STRING_TYPE_H_HEADER #endif // _STRING_TYPE_H_HEADER
Loading…
Cancel
Save