From 5ff8cefca4c3fe73f5daed44ba89d789eba7a5b0 Mon Sep 17 00:00:00 2001 From: mniip Date: Thu, 3 May 2018 04:11:43 +0300 Subject: [PATCH] Replace String::Stream parsing with number splitting --- src/Format.h | 8 ++---- src/common/String.h | 10 ++++---- src/gui/game/PropertyTool.cpp | 45 ++++++++++++---------------------- src/lua/TPTSTypes.cpp | 12 ++++----- src/lua/TPTScriptInterface.cpp | 14 +++++------ 5 files changed, 34 insertions(+), 55 deletions(-) diff --git a/src/Format.h b/src/Format.h index 19cecf848..b29749bde 100644 --- a/src/Format.h +++ b/src/Format.h @@ -18,9 +18,7 @@ namespace format template String NumberToString(T number) { - String::Stream ss; - ss << number; - return ss.str(); + return String::Build(number); } template T ByteStringToNumber(const ByteString & text) @@ -32,9 +30,7 @@ namespace format template T StringToNumber(const String & text) { - String::Stream ss(text); - T number; - return (ss >> number)?number:0; + return text.ToNumber(true); } ByteString URLEncode(ByteString value); diff --git a/src/common/String.h b/src/common/String.h index c13e5d385..24715acb8 100644 --- a/src/common/String.h +++ b/src/common/String.h @@ -163,7 +163,7 @@ public: 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 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(String const &other) const { return super::find(other) != npos; } @@ -229,20 +229,20 @@ public: template inline Split SplitNumber(float &ref, Format::FlagsOverride, size_t pos = 0) const { return SplitFloat(ref, pos, set, reset); } template inline Split SplitNumber(double &ref, Format::FlagsOverride, size_t pos = 0) const { return SplitFloat(ref, pos, set, reset); } - template T ToNumber() const + template T ToNumber(bool noThrow = false) const { T value = T(); Split split = SplitNumber(value); if(split.PositionBefore() != size()) - return T(); + return noThrow ? T() : throw std::runtime_error("Not a number"); return value; } - template inline T ToNumber(Format::FlagsOverride fmt) const + template inline T ToNumber(Format::FlagsOverride fmt, bool noThrow = false) const { T value = T(); Split split = SplitNumber(value, fmt); if(split.PositionBefore() != size()) - return T(); + return noThrow ? T() : throw std::runtime_error("Not a number"); return value; } diff --git a/src/gui/game/PropertyTool.cpp b/src/gui/game/PropertyTool.cpp index 487ab3528..ac762159a 100644 --- a/src/gui/game/PropertyTool.cpp +++ b/src/gui/game/PropertyTool.cpp @@ -107,18 +107,12 @@ void PropertyWindow::SetProperty() if(value.length() > 2 && value.BeginsWith("0x")) { //0xC0FFEE - String::Stream buffer; - buffer.exceptions(String::Stream::failbit | String::Stream::badbit); - buffer << std::hex << value.Substr(2); - buffer >> v; + v = value.Substr(2).ToNumber(Format::Hex()); } else if(value.length() > 1 && value.BeginsWith("0")) { //#C0FFEE - String::Stream buffer; - buffer.exceptions(String::Stream::failbit | String::Stream::badbit); - buffer << std::hex << value.Substr(1); - buffer >> v; + v = value.Substr(1).ToNumber(Format::Hex()); } else { @@ -133,9 +127,7 @@ void PropertyWindow::SetProperty() } else { - String::Stream buffer(value); - buffer.exceptions(String::Stream::failbit | String::Stream::badbit); - buffer >> v; + v = value.ToNumber(); } } @@ -158,24 +150,16 @@ void PropertyWindow::SetProperty() if(value.length() > 2 && value.BeginsWith("0x")) { //0xC0FFEE - String::Stream buffer; - buffer.exceptions(String::Stream::failbit | String::Stream::badbit); - buffer << std::hex << value.Substr(2); - buffer >> v; + v = value.Substr(2).ToNumber(Format::Hex()); } else if(value.length() > 1 && value.BeginsWith("#")) { //#C0FFEE - String::Stream buffer; - buffer.exceptions(String::Stream::failbit | String::Stream::badbit); - buffer << std::hex << value.Substr(1); - buffer >> v; + v = value.Substr(1).ToNumber(Format::Hex()); } else { - String::Stream buffer(value); - buffer.exceptions(String::Stream::failbit | String::Stream::badbit); - buffer >> v; + v = value.ToNumber(); } #ifdef DEBUG std::cout << "Got uint value " << v << std::endl; @@ -185,16 +169,17 @@ void PropertyWindow::SetProperty() } case StructProperty::Float: { - String::Stream buffer(value); - 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")) { - if (value.EndsWith("C")) - tool->propValue.Float += 273.15; - else if (value.EndsWith("F")) - tool->propValue.Float = (tool->propValue.Float-32.0f)*5/9+273.15f; + float v = value.SubstrFromEnd(1).ToNumber(); + tool->propValue.Float = v + 273.15; } + else if(value.EndsWith("F")) + { + float v = value.SubstrFromEnd(1).ToNumber(); + tool->propValue.Float = (v-32.0f)*5/9+273.15f; + } + tool->propValue.Float = value.ToNumber(); #ifdef DEBUG std::cout << "Got float value " << tool->propValue.Float << std::endl; #endif diff --git a/src/lua/TPTSTypes.cpp b/src/lua/TPTSTypes.cpp index 0d77293c7..9615afbf6 100644 --- a/src/lua/TPTSTypes.cpp +++ b/src/lua/TPTSTypes.cpp @@ -71,13 +71,13 @@ AnyType::operator PointType() } else if(type == TypeString) { - String::Stream pointStream(*(value.str)); int x, y; - String::value_type comma; - pointStream >> x >> comma >> y; - if (pointStream.fail() || comma != ',') - throw InvalidConversionException(type, TypePoint); - return PointType(ui::Point(x, y)); + if(String::Split comma = (*value.str).SplitNumber(x)) + if(comma.After().BeginsWith(",")) + if(String::Split end = comma.After().Substr(1).SplitNumber(y)) + if(!end.After().size()) + return PointType(x, y); + throw InvalidConversionException(type, TypePoint); } else throw InvalidConversionException(type, TypePoint); diff --git a/src/lua/TPTScriptInterface.cpp b/src/lua/TPTScriptInterface.cpp index aa5f9de6f..7748dd535 100644 --- a/src/lua/TPTScriptInterface.cpp +++ b/src/lua/TPTScriptInterface.cpp @@ -150,10 +150,7 @@ int TPTScriptInterface::parseNumber(String str) } else { - int number; - String::Stream ss(str); - ss >> number; - return number; + return str.ToNumber(); } return currentNumber; } @@ -188,11 +185,12 @@ AnyType TPTScriptInterface::eval(std::deque * words) return FloatType(atof(word.ToUtf8().c_str())); case TypePoint: { - String::Stream pointStream(word); int x, y; - String::value_type comma; - pointStream >> x >> comma >> y; - return PointType(x, y); + if(String::Split comma = word.SplitNumber(x)) + if(comma.After().BeginsWith(",")) + if(comma.After().Substr(1).SplitNumber(y)) + return PointType(x, y); + return PointType(0, 0); } case TypeString: return StringType(word);