Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
android /platform /bionic /refs/heads/main /. /libc /bionic /wcstod.cpp
blob: 00c8a083d7acc4689d9486670e845d4ba60dee9b [file] [log] [blame]
Elliott Hughes7f0849f2016-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>
Dan Albert1c78cb02017-10-11 11:25:25 -0700[diff] [blame]32#include<string.h>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700[diff] [blame]33
34#include"local.h"
35
Dan Albertf6346552016-12-02 12:02:03 -0800[diff] [blame]36/// Performs wide-character string to floating point conversion.
37template<typename float_type>
38float_type wcstod(constwchar_t* str,wchar_t** end, float_type strtod_fn(constchar*,char**)){
39constwchar_t* original_str= str;
40while(iswspace(*str)){
41 str++;
42}
43
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700[diff] [blame]44// What's the longest span of the input that might be part of the float?
45size_t max_len= wcsspn(str, L"-+0123456789.xXeEpP()nNaAiIfFtTyY");
46
47// We know the only valid characters are ASCII, so convert them by brute force.
48char* ascii_str=newchar[max_len+1];
49if(!ascii_str)return float_type();
50for(size_t i=0; i< max_len;++i){
51 ascii_str[i]= str[i]&0xff;
52}
53 ascii_str[max_len]=0;
54
55// Set up a fake FILE that points to those ASCII characters, for `parsefloat`.
56FILE f;
57 __sfileext fext;
58 _FILEEXT_SETUP(&f,&fext);
59 f._flags= __SRD;
60 f._bf._base= f._p=reinterpret_cast<unsignedchar*>(ascii_str);
61 f._bf._size= f._r= max_len;
62 f._read=[](void*,char*,int){return0;};// aka `eofread`, aka "no more data".
Yi Kong32bc0fc2018-08-02 17:31:13 -0700[diff] [blame]63 f._lb._base=nullptr;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700[diff] [blame]64
65// Ask `parsefloat` to look at the same data more carefully.
66
67// We can't just do this straight away because we can't construct a suitable FILE*
68// in the absence of any `fwmemopen` analogous to `fmemopen`. And we don't want to
69// duplicate the `parsefloat` logic. We also don't want to actually have to have wchar_t
70// implementations of the ASCII `strtod` logic (though if you were designing a libc
71// from scratch, you'd probably want to just make that more generic and lose all the
72// cruft on top).
73size_t actual_len= parsefloat(&f, ascii_str, ascii_str+ max_len);
74
75// Finally let the ASCII conversion function do the work.
76char* ascii_end;
77 float_type result= strtod_fn(ascii_str,&ascii_end);
78if(ascii_end!= ascii_str+ actual_len) abort();
79
Dan Albertf6346552016-12-02 12:02:03 -0800[diff] [blame]80if(end){
81if(actual_len==0){
82// There was an error. We need to set the end pointer back to the original string, not the
83// one we advanced past the leading whitespace.
84*end=const_cast<wchar_t*>(original_str);
85}else{
86*end=const_cast<wchar_t*>(str)+ actual_len;
87}
88}
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700[diff] [blame]89
90delete[] ascii_str;
91return result;
92}
93
94float wcstof(constwchar_t* s,wchar_t** end){
95return wcstod<float>(s, end, strtof);
96}
Elliott Hughes1fc68c72024-08-13 15:48:00 +0000[diff] [blame]97__strong_alias(wcstof_l, wcstof);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700[diff] [blame]98
99double wcstod(constwchar_t* s,wchar_t** end){
100return wcstod<double>(s, end, strtod);
101}
Elliott Hughes1fc68c72024-08-13 15:48:00 +0000[diff] [blame]102__strong_alias(wcstod_l, wcstod);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700[diff] [blame]103
104longdouble wcstold(constwchar_t* s,wchar_t** end){
105return wcstod<longdouble>(s, end, strtold);
106}
Elliott Hughes1fc68c72024-08-13 15:48:00 +0000[diff] [blame]107__strong_alias(wcstold_l, wcstold);

[8]ページ先頭

©2009-2025 Movatter.jp