MCPcopy Create free account
hub / github.com/anjo76/angelscript / parseFloat

Function parseFloat

sdk/add_on/scriptstdstring/scriptstdstring.cpp:906–951  ·  view source on GitHub ↗

AngelScript signature: double parseFloat(const string &in val, uint &out byteCount = 0)

Source from the content-addressed store, hash-verified

904// AngelScript signature:
905// double parseFloat(const string &in val, uint &out byteCount = 0)
906double parseFloat(const string &val, asUINT *byteCount)
907{
908 char *end;
909
910 // Set the locale to C so that we are guaranteed to parse the float value correctly
911#if defined(_WIN32)
912 // WinCE doesn't have setlocale. Some quick testing on my current platform
913 // still manages to parse the numbers such as "3.14" even if the decimal for the
914 // locale is ",".
915#if !defined(_WIN32_WCE)
916 // On Windows setlocale is made threadsafe by turning on thread local setlocale
917 // ref: https://learn.microsoft.com/en-us/cpp/parallel/multithreading-and-locales?view=msvc-170&redirectedfrom=MSDN
918 int oldConfig = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
919 char* tmp = setlocale(LC_NUMERIC, 0);
920 string orig = tmp ? tmp : "C";
921 setlocale(LC_NUMERIC, "C");
922#endif
923#else
924#if !defined(ANDROID) && !defined(__psp2__)
925 // On Linux and other similar systems the threadsafe option is uselocale
926 // ref: https://stackoverflow.com/questions/4057319/is-setlocale-thread-safe-function
927 locale_t locale = newlocale(LC_NUMERIC_MASK, "C", NULL);
928 locale_t orig_locale = uselocale(locale);
929#endif
930#endif
931
932 double res = strtod(val.c_str(), &end);
933
934 // Restore the original locale
935#if defined(_WIN32)
936#if !defined(_WIN32_WCE)
937 setlocale(LC_NUMERIC, orig.c_str());
938 _configthreadlocale(oldConfig);
939#endif
940#else
941#if !defined(ANDROID) && !defined(__psp2__)
942#endif
943 uselocale(orig_locale);
944 freelocale(locale);
945#endif
946
947 if( byteCount )
948 *byteCount = asUINT(size_t(end - val.c_str()));
949
950 return res;
951}
952
953// This function returns a string containing the substring of the input string
954// determined by the starting index and count of characters.

Callers 1

parseFloat_GenericFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected