diff --git a/etk/UString.cpp b/etk/UString.cpp index faaf7e6..0046a6d 100644 --- a/etk/UString.cpp +++ b/etk/UString.cpp @@ -662,6 +662,7 @@ bool etk::UString::ToBool(void) const } return false; } + int64_t etk::UString::ToInt64(void) const { int64_t ret=0; @@ -688,6 +689,28 @@ int64_t etk::UString::ToInt64(void) const return ret; } +uint64_t etk::UString::ToUInt64(void) const +{ + uint64_t ret=0; + for (int32_t iii=0; iii='0' && m_data[iii]<='9') { + int32_t val = m_data[iii] - '0'; + ret = ret*10 + val; + } else { + break; + } + } + } + return ret; +} + int32_t etk::UString::ToInt32(void) const { int64_t parse = ToInt64(); @@ -704,6 +727,22 @@ int8_t etk::UString::ToInt8(void) const return etk_avg((int64_t)INT8_MIN, parse, (int64_t)INT8_MAX); } +uint32_t etk::UString::ToUInt32(void) const +{ + uint64_t parse = ToUInt64(); + return etk_avg((int64_t)0, parse, (int64_t)UINT32_MAX); +} +uint16_t etk::UString::ToUInt16(void) const +{ + uint64_t parse = ToUInt64(); + return etk_avg((int64_t)0, parse, (int64_t)UINT16_MAX); +} +uint8_t etk::UString::ToUInt8(void) const +{ + uint64_t parse = ToUInt64(); + return etk_avg((int64_t)0, parse, (int64_t)UINT8_MAX); +} + double etk::UString::ToDouble(void) const { double ret=0; diff --git a/etk/UString.h b/etk/UString.h index e232b2d..2761a14 100644 --- a/etk/UString.h +++ b/etk/UString.h @@ -148,6 +148,26 @@ namespace etk * @return the requested int */ int8_t ToInt8(void) const; + /** + * @brief Transform the current string in an uint64_t + * @return the requested int + */ + uint64_t ToUInt64(void) const; + /** + * @brief Transform the current string in an uint32_t (if the number is higher, then it is limited at the uint32_t max) + * @return the requested int + */ + uint32_t ToUInt32(void) const; + /** + * @brief Transform the current string in an uint16_t (if the number is higher, then it is limited at the uint16_t max) + * @return the requested int + */ + uint16_t ToUInt16(void) const; + /** + * @brief Transform the current string in an uint8_t (if the number is higher, then it is limited at the uint8_t max) + * @return the requested int + */ + uint8_t ToUInt8(void) const; /** * @brief Transform the current string in a double * @return the requested double diff --git a/etk/math/Vector2D.cpp b/etk/math/Vector2D.cpp index bfabfe0..239dbe5 100644 --- a/etk/math/Vector2D.cpp +++ b/etk/math/Vector2D.cpp @@ -66,95 +66,26 @@ namespace etk { m_floats[0] = false; m_floats[1] = false; + // copy to permit to modify it : + etk::UString tmpStr = _str; if (_str.StartWith("(")) { - if (_str.StartWith("(t")) { - if (_str.CompareNoCase("(true,true)")) { - m_floats[0] = true; - m_floats[1] = true; - } else if (_str.CompareNoCase("(true,false)")) { - m_floats[0] = true; - m_floats[1] = false; - } else if (_str.CompareNoCase("(true)")) { - m_floats[0] = true; - m_floats[1] = true; - } - } else if (_str.StartWith("(f")) { - if (_str.CompareNoCase("(false,true)")) { - m_floats[0] = false; - m_floats[1] = true; - } else if (_str.CompareNoCase("(false,false)")) { - m_floats[0] = false; - m_floats[1] = false; - } else if (_str.CompareNoCase("(false)")) { - m_floats[0] = false; - m_floats[1] = false; - } - } else { - if (_str.CompareNoCase("(1,1)")) { - m_floats[0] = true; - m_floats[1] = true; - } else if (_str.CompareNoCase("(1)")) { - m_floats[0] = true; - m_floats[1] = true; - } else if (_str.CompareNoCase("(1,0)")) { - m_floats[0] = true; - m_floats[1] = false; - } else if (_str.CompareNoCase("(0,1)")) { - m_floats[0] = false; - m_floats[1] = true; - } else if (_str.CompareNoCase("(0,0)")) { - m_floats[0] = false; - m_floats[1] = false; - } else if (_str.CompareNoCase("(0)")) { - m_floats[0] = false; - m_floats[1] = false; - } - } - } else { - if (_str.StartWith("t")) { - if (_str.CompareNoCase("true,true")) { - m_floats[0] = true; - m_floats[1] = true; - } else if (_str.CompareNoCase("true,false")) { - m_floats[0] = true; - m_floats[1] = false; - } else if (_str.CompareNoCase("true")) { - m_floats[0] = true; - m_floats[1] = true; - } - } else if (_str.StartWith("f")) { - if (_str.CompareNoCase("false,true")) { - m_floats[0] = false; - m_floats[1] = true; - } else if (_str.CompareNoCase("false,false")) { - m_floats[0] = false; - m_floats[1] = false; - } else if (_str.CompareNoCase("false")) { - m_floats[0] = false; - m_floats[1] = false; - } - } else { - if (_str.CompareNoCase("1,1")) { - m_floats[0] = true; - m_floats[1] = true; - } else if (_str.CompareNoCase("1")) { - m_floats[0] = true; - m_floats[1] = true; - } else if (_str.CompareNoCase("1,0")) { - m_floats[0] = true; - m_floats[1] = false; - } else if (_str.CompareNoCase("0,1")) { - m_floats[0] = false; - m_floats[1] = true; - } else if (_str.CompareNoCase("0,0")) { - m_floats[0] = false; - m_floats[1] = false; - } else if (_str.CompareNoCase("0")) { - m_floats[0] = false; - m_floats[1] = false; - } - } + tmpStr.Remove(0,1); } + if (tmpStr.EndWith(")")) { + tmpStr.Remove(tmpStr.Size()-1,1); + } + int32_t posComa = tmpStr.FindForward(','); + if (posComa <= 0) { + // no coma ... + // in every case, we parse the first element : + m_floats[0] = tmpStr.ToBool(); + m_floats[1] = m_floats[0]; + } else { + m_floats[0] = tmpStr.Extract(0,posComa).ToBool(); + tmpStr.Remove(0,posComa+1); + m_floats[1] = tmpStr.ToBool(); + } + TK_VERBOSE("Parse : \"" << _str << "\" ==> " << *this); } template<> Vector2D::operator etk::UString(void) const @@ -172,19 +103,27 @@ namespace etk { m_floats[0] = 0; m_floats[1] = 0; + // copy to permit to modify it : + etk::UString tmpStr = _str; if (_str.StartWith("(")) { - int32_t val1=0; - int32_t val2=0; - sscanf(_str.c_str(), "(%d,%d)", &val1, &val2); - m_floats[0] = val1; - m_floats[1] = val2; - } else { - int32_t val1=0; - int32_t val2=0; - sscanf(_str.c_str(), "%d,%d", &val1, &val2); - m_floats[0] = val1; - m_floats[1] = val2; + tmpStr.Remove(0,1); } + if (tmpStr.EndWith(")")) { + tmpStr.Remove(tmpStr.Size()-1,1); + } + + int32_t posComa = tmpStr.FindForward(','); + if (posComa <= 0) { + // no coma ... + // in every case, we parse the first element : + m_floats[0] = tmpStr.ToInt32(); + m_floats[1] = m_floats[0]; + } else { + m_floats[0] = tmpStr.Extract(0,posComa).ToInt32(); + tmpStr.Remove(0,posComa+1); + m_floats[1] = tmpStr.ToInt32(); + } + TK_VERBOSE("Parse : \"" << _str << "\" ==> " << *this); } template<> Vector2D::operator etk::UString(void) const @@ -202,19 +141,26 @@ namespace etk { m_floats[0] = 0; m_floats[1] = 0; + // copy to permit to modify it : + etk::UString tmpStr = _str; if (_str.StartWith("(")) { - int32_t val1=0; - int32_t val2=0; - sscanf(_str.c_str(), "(%d,%d)", &val1, &val2); - m_floats[0] = etk_max(0,val1); - m_floats[1] = etk_max(0,val2); - } else { - int32_t val1=0; - int32_t val2=0; - sscanf(_str.c_str(), "%d,%d", &val1, &val2); - m_floats[0] = etk_max(0,val1); - m_floats[1] = etk_max(0,val2); + tmpStr.Remove(0,1); } + if (tmpStr.EndWith(")")) { + tmpStr.Remove(tmpStr.Size()-1,1); + } + int32_t posComa = tmpStr.FindForward(','); + if (posComa <= 0) { + // no coma ... + // in every case, we parse the first element : + m_floats[0] = tmpStr.ToUInt32(); + m_floats[1] = m_floats[0]; + } else { + m_floats[0] = tmpStr.Extract(0,posComa).ToUInt32(); + tmpStr.Remove(0,posComa+1); + m_floats[1] = tmpStr.ToUInt32(); + } + TK_VERBOSE("Parse : \"" << _str << "\" ==> " << *this); } template<> Vector2D::operator etk::UString(void) const @@ -232,14 +178,26 @@ namespace etk { m_floats[0] = 0; m_floats[1] = 0; - //TK_DEBUG("start parsing od vec2 with \"" << str << "\""); + // copy to permit to modify it : + etk::UString tmpStr = _str; if (_str.StartWith("(")) { - //TK_DEBUG(" start with ("); - sscanf(_str.c_str(), "(%f,%f)", &m_floats[0], &m_floats[1]); - } else { - sscanf(_str.c_str(), "%f,%f", &m_floats[0], &m_floats[1]); + tmpStr.Remove(0,1); } - //TK_DEBUG(" result " << *this); + if (tmpStr.EndWith(")")) { + tmpStr.Remove(tmpStr.Size()-1,1); + } + int32_t posComa = tmpStr.FindForward(','); + if (posComa <= 0) { + // no coma ... + // in every case, we parse the first element : + m_floats[0] = tmpStr.ToFloat(); + m_floats[1] = m_floats[0]; + } else { + m_floats[0] = tmpStr.Extract(0,posComa).ToFloat(); + tmpStr.Remove(0,posComa+1); + m_floats[1] = tmpStr.ToFloat(); + } + TK_VERBOSE("Parse : \"" << _str << "\" ==> " << *this); } };