diff --git a/ZString.cpp b/ZString.cpp index 9f45f5b..6387e08 100644 --- a/ZString.cpp +++ b/ZString.cpp @@ -1,4 +1,3 @@ - #include "ZString.h" #include "Log.h" #include "Exception.h" @@ -125,7 +124,56 @@ namespace coreutils { } double ZString::asDouble() { - return strtod(cursor, &cursor); + double value = 0; + bool negative = false; + char *end = data + length; + if(*cursor == '-') { + negative = true; + ++cursor; + } else if(*cursor == '+') + ++cursor; + if(cursor == end) + return 0; + while((*cursor >= '0') && (*cursor <= '9')) { + value *= 10; + value += *cursor - 48; + ++cursor; + if(cursor == end) { + if(negative) + return -value; + else + return value; + } + } + if(*cursor != '.') + if(cursor == end) { + if(negative) + return -value; + else + return value; + } + ++cursor; + if(cursor == end) { + if(negative) + return -value; + else + return value; + } + double div = 10; + while((*cursor >= '0') && (*cursor <= '9')) { + value += (*cursor - 48) / div; + div *= 10; + ++cursor; + if(cursor == end) { + if(negative) + return -value; + else + return value; + } + } + if(negative) + return -value; + return value; } int ZString::incrementCursor(int length) { diff --git a/testing/zstring_test.cpp b/testing/zstring_test.cpp index 974af57..fe8cf6b 100644 --- a/testing/zstring_test.cpp +++ b/testing/zstring_test.cpp @@ -99,5 +99,8 @@ int main(int argc, char **argv) { std::cout << "[" << test18.find(".") << "] " << test18.parsed() << ":" << test18.unparsed() << std::endl; test18.reset(); std::cout << "[" << test18.find("x") << "] " << test18.parsed() << ":" << test18.unparsed() << std::endl; + + coreutils::ZString test19("-16473.65476Z"); + std::cout << test19.asDouble() << ":" << test19.unparsed() << std::endl; }