1
0
Fork 0

DBG: updated UString class (removed some usless stuff)

This commit is contained in:
Mr. eXoDia 2014-09-11 21:16:03 +02:00
parent c79bc82292
commit 003150a4e5
4 changed files with 7 additions and 209 deletions

View File

@ -22,83 +22,6 @@
#include <errno.h> #include <errno.h>
#include "Exception.h" #include "Exception.h"
void UTF8::String::ConvertFromDouble(const long double d, const UTF8::String & ThousandSeparator, const UTF8::String & FractionSeparator, const int IntegerPartLength, const int FractionPartLength)
{
std::ostringstream os;
os.precision(15);
os << d;
UTF8::String Number(os.str());
UTF8::String Integer, Fraction;
// Extracting integer and fraction
std::vector <UTF8::String> Extracted = Number.Explode(".");
unsigned int IntegerLength;
if(IntegerPartLength)
{
IntegerLength = IntegerPartLength;
}
else
{
IntegerLength = Extracted[0].Length();
}
unsigned int FractionLength;
if(FractionPartLength)
{
FractionLength = FractionPartLength;
}
else
{
if(Extracted.size() > 1)
{
FractionLength = Extracted[1].Length();
}
else
{
FractionLength = 0;
}
}
// Parsing integer
for(unsigned int i = 0; i < IntegerLength; i++)
{
if((i > 0) && (i % 3 == 0))
{
Integer = ThousandSeparator + Integer;
}
if(Extracted[0].Length() < i + 1)
{
Integer = "0" + Integer;
}
else
{
Integer = Extracted[0][Extracted[0].Length() - 1 - i] + Integer;
}
}
// Parsing fraction
if(FractionLength)
{
Fraction = FractionSeparator;
for(unsigned int i = 0; i < FractionLength; i++)
{
if((Extracted.size() > 1) && (Extracted[1].Length() > i))
{
Fraction += Extracted[1][i];
}
else
{
Fraction += "0";
}
}
}
* this = Integer + Fraction;
}
bool UTF8::String::HasThisString(const UTF8::String & Str) const bool UTF8::String::HasThisString(const UTF8::String & Str) const
{ {
return GetSubstringPosition(Str) != -1; return GetSubstringPosition(Str) != -1;
@ -227,7 +150,7 @@ bool operator!=(const std::string & str, const UTF8::String & StringObj)
UTF8::String UTF8::String::Quote() const UTF8::String UTF8::String::Quote() const
{ {
return "«" + (*this) + "»"; return "\"" + (*this) + "\"";
} }
UTF8::String UTF8::String::Trim() const UTF8::String UTF8::String::Trim() const
@ -357,14 +280,6 @@ UTF8::String operator+(const std::string & str, const UTF8::String & StringObj)
return s; return s;
} }
UTF8::String operator+(const long l, const UTF8::String & StringObj)
{
UTF8::String s(l);
s += StringObj;
return s;
}
UTF8::String UTF8::String::operator+(const UTF8::String & s) const UTF8::String UTF8::String::operator+(const UTF8::String & s) const
{ {
UTF8::String res(*this); UTF8::String res(*this);
@ -497,12 +412,6 @@ void UTF8::String::ConvertFromInt64(int64_t n)
CalculateStringLength(); CalculateStringLength();
} }
UTF8::String::String(const long double d, const UTF8::String & ThousandSeparator, const UTF8::String & DecimalSeparator, const int IntegerPartCount, const int FractionPartCount)
{
InitString();
ConvertFromDouble(d, ThousandSeparator, DecimalSeparator, IntegerPartCount, FractionPartCount);
}
void UTF8::String::InitString() void UTF8::String::InitString()
{ {
Data = NULL; Data = NULL;
@ -527,7 +436,7 @@ int UTF8::String::GetSymbolIndexInDataArray(unsigned int Position) const
{ {
if(Position >= StringLength) if(Position >= StringLength)
{ {
throw Exception((UTF8::String("[GetSymbolIndexInDataArray] trying to get position beyond the end of string. StringLength: ") + StringLength + " Position: " + Position + " String: [" + Data + "]").ToString()); throw Exception(UTF8::String("[GetSymbolIndexInDataArray] trying to get position beyond the end of string."));
} }
unsigned int n = 0; unsigned int n = 0;
@ -851,14 +760,6 @@ UTF8::String & UTF8::String::operator=(const uint32_t* str)
return *this; return *this;
} }
UTF8::String & UTF8::String::operator=(long double d)
{
Empty();
ConvertFromDouble(d);
return *this;
}
UTF8::String::operator std::string() const UTF8::String::operator std::string() const
{ {
return this->ToString(); return this->ToString();
@ -885,86 +786,6 @@ std::string UTF8::String::ToString() const
} }
} }
double UTF8::String::ToDouble() const
{
int64_t mul = 1;
char c;
int int_part = 0;
double prec_part = 0;
for(int i = DataArrayLength - 1; i >= 0; i--)
{
c = Data[i];
if((c >= '0') && (c <= '9'))
{
int_part += (c - '0') * (int)mul;
mul *= 10;
}
else
{
if(c == '.')
{
prec_part = (double) int_part / (double) mul;
int_part = 0;
mul = 1;
}
else
{
if((c == '-') && (i == 0))
{
int_part = -int_part;
prec_part = -prec_part;
}
else
{
UTF8::String err = "Cannot convert \"" + * this + "\" to double.";
throw UTF8::Exception(err.ToConstCharPtr(), UTF8::Exception::StringToDoubleConversionError);
}
}
}
}
return int_part + prec_part;
}
int64_t UTF8::String::ToLong() const
{
int64_t mul = 1;
char c;
int64_t number = 0;
for(int i = DataArrayLength - 1; i >= 0; i--)
{
c = Data[i];
if((c >= '0') && (c <= '9'))
{
number += (c - '0') * mul;
mul *= 10;
}
else
{
if(c == '.')
{
number = 0;
mul = 1;
}
else
{
if((c == '-') && (i == 0))
{
number = -number;
}
else
{
UTF8::String err = "Cannot convert \"" + * this + "\" to number.";
throw UTF8::Exception(err.ToConstCharPtr(), UTF8::Exception::StringToIntConversionError);
}
}
}
}
return number;
}
UTF8::String UTF8::String::operator+(const char* s) const UTF8::String UTF8::String::operator+(const char* s) const
{ {
UTF8::String res(*this); UTF8::String res(*this);

View File

@ -64,10 +64,8 @@ public:
String(const String & orig); String(const String & orig);
/** /**
* Converting from long constructor. Automatically generates string from number. * Deconstructor.
*/ */
String(const long double d, const UTF8::String & ThousandSeparator = "", const UTF8::String & FractionSeparator = ".", const int IntegerPartLength = 0, const int FractionPartLength = 0);
~String(); ~String();
/** /**
@ -80,16 +78,6 @@ public:
*/ */
static String FromFile(const UTF8::String & Path); static String FromFile(const UTF8::String & Path);
/**
* Converts UTF8::String to long
*/
int64_t ToLong() const;
/**
* Converts UTF8::String to double
*/
double ToDouble() const;
/** /**
* Converts UTF8::String to const char * * Converts UTF8::String to const char *
*/ */
@ -141,11 +129,6 @@ public:
*/ */
String & operator=(const uint32_t*); String & operator=(const uint32_t*);
/**
* Assign operator. Provides String1=(long double) expression.
*/
String & operator=(long double);
/** /**
* Provides std::string test=String expression. * Provides std::string test=String expression.
*/ */
@ -282,7 +265,6 @@ private:
int GetSymbolIndexInDataArray(unsigned int Position) const; int GetSymbolIndexInDataArray(unsigned int Position) const;
void ConvertFromUTF32(const uint32_t*); void ConvertFromUTF32(const uint32_t*);
void ConvertFromDouble(const long double d, const UTF8::String & ThousandSeparator = "", const UTF8::String & DecimalSeparator = ".", const int IntegerPartCount = 0, const int FractionPartCount = 0);
}; };
} }
@ -297,11 +279,6 @@ UTF8::String operator+(const char*, const UTF8::String &);
*/ */
UTF8::String operator+(const std::string &, const UTF8::String &); UTF8::String operator+(const std::string &, const UTF8::String &);
/**
* Not in class overloaded operator +. Provides 123+String1 expression.
*/
UTF8::String operator+(long, const UTF8::String &);
/** /**
* Not in class overloaded operator ==. Provides "Test"==String1 expression. * Not in class overloaded operator ==. Provides "Test"==String1 expression.
*/ */

View File

@ -2,7 +2,7 @@
#include <windows.h> #include <windows.h>
//Functions taken from: http://www.nubaria.com/en/blog/?p=289 //Functions taken from: http://www.nubaria.com/en/blog/?p=289
std::string ConvertUtf16ToUtf8(const std::wstring & wstr) UString ConvertUtf16ToUtf8(const std::wstring & wstr)
{ {
std::string convertedString; std::string convertedString;
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, 0, 0, 0, 0); int requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, 0, 0, 0, 0);
@ -15,7 +15,7 @@ std::string ConvertUtf16ToUtf8(const std::wstring & wstr)
return convertedString; return convertedString;
} }
std::wstring ConvertUtf8ToUtf16(const std::string & str) std::wstring ConvertUtf8ToUtf16(const UString & str)
{ {
std::wstring convertedString; std::wstring convertedString;
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0); int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0);

View File

@ -6,7 +6,7 @@
typedef UTF8::String UString; typedef UTF8::String UString;
std::string ConvertUtf16ToUtf8(const std::wstring & wstr); UString ConvertUtf16ToUtf8(const std::wstring & wstr);
std::wstring ConvertUtf8ToUtf16(const std::string & str); std::wstring ConvertUtf8ToUtf16(const UString & str);
#endif // _USTRING_H #endif // _USTRING_H