| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2016 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <wchar.h> | 
|  | 30 |  | 
|  | 31 | #include <stdlib.h> | 
|  | 32 |  | 
|  | 33 | #include "local.h" | 
|  | 34 |  | 
| Dan Albert | f634655 | 2016-12-02 12:02:03 -0800 | [diff] [blame] | 35 | /// Performs wide-character string to floating point conversion. | 
|  | 36 | template <typename float_type> | 
|  | 37 | float_type wcstod(const wchar_t* str, wchar_t** end, float_type strtod_fn(const char*, char**)) { | 
|  | 38 | const wchar_t* original_str = str; | 
|  | 39 | while (iswspace(*str)) { | 
|  | 40 | str++; | 
|  | 41 | } | 
|  | 42 |  | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 43 | // What's the longest span of the input that might be part of the float? | 
|  | 44 | size_t max_len = wcsspn(str, L"-+0123456789.xXeEpP()nNaAiIfFtTyY"); | 
|  | 45 |  | 
|  | 46 | // We know the only valid characters are ASCII, so convert them by brute force. | 
|  | 47 | char* ascii_str = new char[max_len + 1]; | 
|  | 48 | if (!ascii_str) return float_type(); | 
|  | 49 | for (size_t i = 0; i < max_len; ++i) { | 
|  | 50 | ascii_str[i] = str[i] & 0xff; | 
|  | 51 | } | 
|  | 52 | ascii_str[max_len] = 0; | 
|  | 53 |  | 
|  | 54 | // Set up a fake FILE that points to those ASCII characters, for `parsefloat`. | 
|  | 55 | FILE f; | 
|  | 56 | __sfileext fext; | 
|  | 57 | _FILEEXT_SETUP(&f, &fext); | 
|  | 58 | f._flags = __SRD; | 
|  | 59 | f._bf._base = f._p = reinterpret_cast<unsigned char*>(ascii_str); | 
|  | 60 | f._bf._size = f._r = max_len; | 
|  | 61 | f._read = [](void*, char*, int) { return 0; }; // aka `eofread`, aka "no more data". | 
|  | 62 | f._lb._base = NULL; | 
|  | 63 |  | 
|  | 64 | // Ask `parsefloat` to look at the same data more carefully. | 
|  | 65 |  | 
|  | 66 | // We can't just do this straight away because we can't construct a suitable FILE* | 
|  | 67 | // in the absence of any `fwmemopen` analogous to `fmemopen`. And we don't want to | 
|  | 68 | // duplicate the `parsefloat` logic. We also don't want to actually have to have wchar_t | 
|  | 69 | // implementations of the ASCII `strtod` logic (though if you were designing a libc | 
|  | 70 | // from scratch, you'd probably want to just make that more generic and lose all the | 
|  | 71 | // cruft on top). | 
|  | 72 | size_t actual_len = parsefloat(&f, ascii_str, ascii_str + max_len); | 
|  | 73 |  | 
|  | 74 | // Finally let the ASCII conversion function do the work. | 
|  | 75 | char* ascii_end; | 
|  | 76 | float_type result = strtod_fn(ascii_str, &ascii_end); | 
|  | 77 | if (ascii_end != ascii_str + actual_len) abort(); | 
|  | 78 |  | 
| Dan Albert | f634655 | 2016-12-02 12:02:03 -0800 | [diff] [blame] | 79 | if (end) { | 
|  | 80 | if (actual_len == 0) { | 
|  | 81 | // There was an error. We need to set the end pointer back to the original string, not the | 
|  | 82 | // one we advanced past the leading whitespace. | 
|  | 83 | *end = const_cast<wchar_t*>(original_str); | 
|  | 84 | } else { | 
|  | 85 | *end = const_cast<wchar_t*>(str) + actual_len; | 
|  | 86 | } | 
|  | 87 | } | 
| Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 88 |  | 
|  | 89 | delete[] ascii_str; | 
|  | 90 | return result; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | float wcstof(const wchar_t* s, wchar_t** end) { | 
|  | 94 | return wcstod<float>(s, end, strtof); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | double wcstod(const wchar_t* s, wchar_t** end) { | 
|  | 98 | return wcstod<double>(s, end, strtod); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | long double wcstold(const wchar_t* s, wchar_t** end) { | 
|  | 102 | return wcstod<long double>(s, end, strtold); | 
|  | 103 | } |