static uint64_t NGS_StringToU64( const NGS_String * str, ctx_t ctx ) { /* have to guarantee NUL-termination for strtou64/strtod */ char buf[4096]; if ( sizeof(buf) > NGS_StringSize ( str, ctx ) ) { char* end; uint64_t value; string_copy ( buf, sizeof(buf), NGS_StringData ( str, ctx ), NGS_StringSize ( str, ctx ) ); errno = 0; value = strtou64 ( buf, &end, 10 ); if ( *end == 0 ) { if ( errno == 0 ) { return value; } } else { /* attempt to parse as a double */ double dbl; errno = 0; dbl = strtod ( buf, &end ); if ( *end == 0 && errno == 0 && dbl >= 0 && dbl <= ULLONG_MAX ) { return ( uint64_t ) xtrunc ( dbl ); } } } INTERNAL_ERROR ( xcUnexpected, "cannot convert dictionary value '%.*s' from string to uint64", NGS_StringSize ( str, ctx ), NGS_StringData ( str, ctx ) ); return 0; }
static double NGS_StringToReal ( const NGS_String * str, ctx_t ctx ) { /* have to guarantee NUL-termination for strtod */ char buf[4096]; if ( sizeof(buf) > NGS_StringSize ( str, ctx ) ) { char* end; double real; string_copy ( buf, sizeof(buf), NGS_StringData ( str, ctx ), NGS_StringSize ( str, ctx ) ); errno = 0; real = strtod ( buf, &end ); if ( *end == 0 && errno == 0 ) { return real; } } INTERNAL_ERROR ( xcUnexpected, "cannot convert dictionary value '%.*s' from string to numeric", NGS_StringSize ( str, ctx ), NGS_StringData ( str, ctx ) ); return 0.0; }
std :: string toString ( const NGS_String* str, ctx_t ctx, bool release_source = false ) { if ( str == 0 ) { throw std :: logic_error ( "toString ( NULL ) called" ); } std :: string ret = std::string ( NGS_StringData ( str, ctx ), NGS_StringSize ( str, ctx ) ); if ( release_source ) { NGS_StringRelease ( str, ctx ); } return ret; }