Replace String::Stream parsing with number splitting

This commit is contained in:
mniip 2018-05-03 04:11:43 +03:00
parent 1ea7a035d1
commit 5ff8cefca4
5 changed files with 34 additions and 55 deletions

View File

@ -18,9 +18,7 @@ namespace format
template <typename T> String NumberToString(T number) template <typename T> String NumberToString(T number)
{ {
String::Stream ss; return String::Build(number);
ss << number;
return ss.str();
} }
template <typename T> T ByteStringToNumber(const ByteString & text) template <typename T> T ByteStringToNumber(const ByteString & text)
@ -32,9 +30,7 @@ namespace format
template <typename T> T StringToNumber(const String & text) template <typename T> T StringToNumber(const String & text)
{ {
String::Stream ss(text); return text.ToNumber<T>(true);
T number;
return (ss >> number)?number:0;
} }
ByteString URLEncode(ByteString value); ByteString URLEncode(ByteString value);

View File

@ -163,7 +163,7 @@ public:
inline String Substr(size_t pos = 0, size_t count = npos) const { return super::substr(pos, count); } inline String Substr(size_t pos = 0, size_t count = npos) const { return super::substr(pos, count); }
inline String SubstrFromEnd(size_t rpos = 0, size_t rcount = npos) const { return super::substr(rcount == npos || rcount > rpos ? 0 : rpos - rcount, size() - rpos); } inline String SubstrFromEnd(size_t rpos = 0, size_t rcount = npos) const { return super::substr(rcount == npos || rcount > rpos ? 0 : rpos - rcount, size() - rpos); }
inline String Between(size_t from, size_t to) const { return from >= to ? String() : super::substr(from, to - from); } inline String Between(size_t from, size_t to) const { return to == npos ? super::substr(from) : from >= to ? String() : super::substr(from, to - from); }
inline bool Contains(value_type ch) const { return super::find(ch) != npos; } inline bool Contains(value_type ch) const { return super::find(ch) != npos; }
inline bool Contains(String const &other) const { return super::find(other) != npos; } inline bool Contains(String const &other) const { return super::find(other) != npos; }
@ -229,20 +229,20 @@ public:
template<std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline Split SplitNumber(float &ref, Format::FlagsOverride<void, set, reset>, size_t pos = 0) const { return SplitFloat(ref, pos, set, reset); } template<std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline Split SplitNumber(float &ref, Format::FlagsOverride<void, set, reset>, size_t pos = 0) const { return SplitFloat(ref, pos, set, reset); }
template<std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline Split SplitNumber(double &ref, Format::FlagsOverride<void, set, reset>, size_t pos = 0) const { return SplitFloat(ref, pos, set, reset); } template<std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline Split SplitNumber(double &ref, Format::FlagsOverride<void, set, reset>, size_t pos = 0) const { return SplitFloat(ref, pos, set, reset); }
template<typename T> T ToNumber() const template<typename T> T ToNumber(bool noThrow = false) const
{ {
T value = T(); T value = T();
Split split = SplitNumber(value); Split split = SplitNumber(value);
if(split.PositionBefore() != size()) if(split.PositionBefore() != size())
return T(); return noThrow ? T() : throw std::runtime_error("Not a number");
return value; return value;
} }
template<typename T, std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline T ToNumber(Format::FlagsOverride<void, set, reset> fmt) const template<typename T, std::ios_base::fmtflags set, std::ios_base::fmtflags reset> inline T ToNumber(Format::FlagsOverride<void, set, reset> fmt, bool noThrow = false) const
{ {
T value = T(); T value = T();
Split split = SplitNumber(value, fmt); Split split = SplitNumber(value, fmt);
if(split.PositionBefore() != size()) if(split.PositionBefore() != size())
return T(); return noThrow ? T() : throw std::runtime_error("Not a number");
return value; return value;
} }

View File

@ -107,18 +107,12 @@ void PropertyWindow::SetProperty()
if(value.length() > 2 && value.BeginsWith("0x")) if(value.length() > 2 && value.BeginsWith("0x"))
{ {
//0xC0FFEE //0xC0FFEE
String::Stream buffer; v = value.Substr(2).ToNumber<unsigned int>(Format::Hex());
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer << std::hex << value.Substr(2);
buffer >> v;
} }
else if(value.length() > 1 && value.BeginsWith("0")) else if(value.length() > 1 && value.BeginsWith("0"))
{ {
//#C0FFEE //#C0FFEE
String::Stream buffer; v = value.Substr(1).ToNumber<unsigned int>(Format::Hex());
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer << std::hex << value.Substr(1);
buffer >> v;
} }
else else
{ {
@ -133,9 +127,7 @@ void PropertyWindow::SetProperty()
} }
else else
{ {
String::Stream buffer(value); v = value.ToNumber<int>();
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer >> v;
} }
} }
@ -158,24 +150,16 @@ void PropertyWindow::SetProperty()
if(value.length() > 2 && value.BeginsWith("0x")) if(value.length() > 2 && value.BeginsWith("0x"))
{ {
//0xC0FFEE //0xC0FFEE
String::Stream buffer; v = value.Substr(2).ToNumber<unsigned int>(Format::Hex());
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer << std::hex << value.Substr(2);
buffer >> v;
} }
else if(value.length() > 1 && value.BeginsWith("#")) else if(value.length() > 1 && value.BeginsWith("#"))
{ {
//#C0FFEE //#C0FFEE
String::Stream buffer; v = value.Substr(1).ToNumber<unsigned int>(Format::Hex());
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer << std::hex << value.Substr(1);
buffer >> v;
} }
else else
{ {
String::Stream buffer(value); v = value.ToNumber<unsigned int>();
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer >> v;
} }
#ifdef DEBUG #ifdef DEBUG
std::cout << "Got uint value " << v << std::endl; std::cout << "Got uint value " << v << std::endl;
@ -185,16 +169,17 @@ void PropertyWindow::SetProperty()
} }
case StructProperty::Float: case StructProperty::Float:
{ {
String::Stream buffer(value); if (value.EndsWith("C"))
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer >> tool->propValue.Float;
if (properties[property->GetOption().second].Name == "temp" && value.length())
{ {
if (value.EndsWith("C")) float v = value.SubstrFromEnd(1).ToNumber<float>();
tool->propValue.Float += 273.15; tool->propValue.Float = v + 273.15;
else if (value.EndsWith("F"))
tool->propValue.Float = (tool->propValue.Float-32.0f)*5/9+273.15f;
} }
else if(value.EndsWith("F"))
{
float v = value.SubstrFromEnd(1).ToNumber<float>();
tool->propValue.Float = (v-32.0f)*5/9+273.15f;
}
tool->propValue.Float = value.ToNumber<float>();
#ifdef DEBUG #ifdef DEBUG
std::cout << "Got float value " << tool->propValue.Float << std::endl; std::cout << "Got float value " << tool->propValue.Float << std::endl;
#endif #endif

View File

@ -71,13 +71,13 @@ AnyType::operator PointType()
} }
else if(type == TypeString) else if(type == TypeString)
{ {
String::Stream pointStream(*(value.str));
int x, y; int x, y;
String::value_type comma; if(String::Split comma = (*value.str).SplitNumber(x))
pointStream >> x >> comma >> y; if(comma.After().BeginsWith(","))
if (pointStream.fail() || comma != ',') if(String::Split end = comma.After().Substr(1).SplitNumber(y))
throw InvalidConversionException(type, TypePoint); if(!end.After().size())
return PointType(ui::Point(x, y)); return PointType(x, y);
throw InvalidConversionException(type, TypePoint);
} }
else else
throw InvalidConversionException(type, TypePoint); throw InvalidConversionException(type, TypePoint);

View File

@ -150,10 +150,7 @@ int TPTScriptInterface::parseNumber(String str)
} }
else else
{ {
int number; return str.ToNumber<int>();
String::Stream ss(str);
ss >> number;
return number;
} }
return currentNumber; return currentNumber;
} }
@ -188,11 +185,12 @@ AnyType TPTScriptInterface::eval(std::deque<String> * words)
return FloatType(atof(word.ToUtf8().c_str())); return FloatType(atof(word.ToUtf8().c_str()));
case TypePoint: case TypePoint:
{ {
String::Stream pointStream(word);
int x, y; int x, y;
String::value_type comma; if(String::Split comma = word.SplitNumber(x))
pointStream >> x >> comma >> y; if(comma.After().BeginsWith(","))
return PointType(x, y); if(comma.After().Substr(1).SplitNumber(y))
return PointType(x, y);
return PointType(0, 0);
} }
case TypeString: case TypeString:
return StringType(word); return StringType(word);