Permalink
Browse files

Replace String::Stream parsing with number splitting

  • Loading branch information...
mniip committed May 3, 2018
1 parent 1ea7a03 commit 5ff8cefca4c3fe73f5daed44ba89d789eba7a5b0
Showing with 34 additions and 55 deletions.
  1. +2 −6 src/Format.h
  2. +5 −5 src/common/String.h
  3. +15 −30 src/gui/game/PropertyTool.cpp
  4. +6 −6 src/lua/TPTSTypes.cpp
  5. +6 −8 src/lua/TPTScriptInterface.cpp
@@ -18,9 +18,7 @@ namespace format
template <typename T> String NumberToString(T number)
{
String::Stream ss;
ss << number;
return ss.str();
return String::Build(number);
}
template <typename T> T ByteStringToNumber(const ByteString & text)
@@ -32,9 +30,7 @@ namespace format
template <typename T> T StringToNumber(const String & text)
{
String::Stream ss(text);
T number;
return (ss >> number)?number:0;
return text.ToNumber<T>(true);
}
ByteString URLEncode(ByteString value);
@@ -163,7 +163,7 @@ class String : public std::basic_string<char32_t>
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 @@ class String : public std::basic_string<char32_t>
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<typename T> T ToNumber() const
template<typename T> 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<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();
Split split = SplitNumber(value, fmt);
if(split.PositionBefore() != size())
return T();
return noThrow ? T() : throw std::runtime_error("Not a number");
return value;
}
@@ -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<unsigned int>(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<unsigned int>(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<int>();
}
}
@@ -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<unsigned int>(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<unsigned int>(Format::Hex());
}
else
{
String::Stream buffer(value);
buffer.exceptions(String::Stream::failbit | String::Stream::badbit);
buffer >> v;
v = value.ToNumber<unsigned int>();
}
#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<float>();
tool->propValue.Float = v + 273.15;
}
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
std::cout << "Got float value " << tool->propValue.Float << std::endl;
#endif
@@ -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);
@@ -150,10 +150,7 @@ int TPTScriptInterface::parseNumber(String str)
}
else
{
int number;
String::Stream ss(str);
ss >> number;
return number;
return str.ToNumber<int>();
}
return currentNumber;
}
@@ -188,11 +185,12 @@ AnyType TPTScriptInterface::eval(std::deque<String> * 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);

0 comments on commit 5ff8cef

Please sign in to comment.