Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
android /platform /bionic /refs/heads/main /. /libc /bionic /strtol.cpp
blob: 607145d7b80e1d60a2e6ddb8dfff86124be7ddef [file] [log] [blame] [edit]
/*
* Copyright (c) 1992 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include<ctype.h>
#include<errno.h>
#include<inttypes.h>
#include<limits.h>
#include<stdlib.h>
#include<wchar.h>
template<typename T, TMin, TMax,typenameCharT>
__attribute__((always_inline)) TStrToI(constCharT* s,CharT** end_ptr,int base){
// Ensure that base is between 2 and 36 inclusive, or the special value of 0.
if(base<0|| base==1|| base>36){
if(end_ptr!=nullptr)*end_ptr=const_cast<CharT*>(s);
errno= EINVAL;
return0;
}
// Skip white space and pick up leading +/- sign if any.
constCharT* p= s;
int c;
while(isspace(c=*p++)){
}
bool neg=false;
if(c=='-'){
neg=true;
c=*p++;
}elseif(c=='+'){
c=*p++;
}
// If base is 0 or 16, allow "0x" prefix for hex.
if((base==0|| base==16)&& c=='0'&&(*p=='x'||*p=='X')&& isxdigit(p[1])){
c= p[1];
p+=2;
base=16;
}
// If base is 0 or 2, allow "0b" prefix for binary.
if((base==0|| base==2)&& c=='0'&&(*p=='b'||*p=='B')&& isdigit(p[1])){
c= p[1];
p+=2;
base=2;
}
// If base is 0, allow "0" prefix for octal, otherwise base is 10.
if(base==0) base=(c=='0')?8:10;
constexprbool is_signed=(Min!=0);
T acc=0;
// Non-zero if any digits consumed; negative to indicate overflow/underflow.
int any=0;
for(;; c=*p++){
if(isdigit(c)){
c-='0';
}elseif(isalpha(c)){
c=10+(_tolower(c)-'a');
}else{
break;
}
if(c>= base)break;
if(any<0)continue;
if(is_signed){
// We work in the negative space because the most negative value has a
// larger magnitude than the most positive value.
if(__builtin_mul_overflow(acc, base,&acc)|| __builtin_sub_overflow(acc, c,&acc)){
any=-1;
continue;
}
}else{
if(__builtin_mul_overflow(acc, base,&acc)|| __builtin_add_overflow(acc, c,&acc)){
any=-1;
continue;
}
}
any=1;
}
if(end_ptr!=nullptr)*end_ptr=const_cast<CharT*>(any? p-1: s);
// Detected overflow/underflow in the loop?
if(any==-1){
errno= ERANGE;
return(is_signed&& neg)?Min:Max;
}
// Will we overflow by trying to negate the most negative value?
if(any>0&& is_signed&&!neg&& acc==Min){
errno= ERANGE;
returnMax;
}
if(is_signed)return neg? acc:-acc;
return neg?-acc: acc;
}
int atoi(constchar* s){
return strtol(s,nullptr,10);
}
long atol(constchar* s){
return strtol(s,nullptr,10);
}
longlong atoll(constchar* s){
return strtoll(s,nullptr,10);
}
intmax_t strtoimax(constchar* s,char** end,int base){
returnStrToI<intmax_t, INTMAX_MIN, INTMAX_MAX,char>(s, end, base);
}
intmax_t wcstoimax(constwchar_t* s,wchar_t** end,int base){
returnStrToI<intmax_t, INTMAX_MIN, INTMAX_MAX,wchar_t>(s, end, base);
}
long strtol(constchar* s,char** end,int base){
returnStrToI<long, LONG_MIN, LONG_MAX,char>(s, end, base);
}
__strong_alias(strtol_l, strtol);
long wcstol(constwchar_t* s,wchar_t** end,int base){
returnStrToI<long, LONG_MIN, LONG_MAX,wchar_t>(s, end, base);
}
__strong_alias(wcstol_l, wcstol);
longlong strtoll(constchar* s,char** end,int base){
returnStrToI<longlong, LLONG_MIN, LLONG_MAX,char>(s, end, base);
}
__strong_alias(strtoll_l, strtoll);
longlong wcstoll(constwchar_t* s,wchar_t** end,int base){
returnStrToI<longlong, LLONG_MIN, LLONG_MAX,wchar_t>(s, end, base);
}
__strong_alias(wcstoll_l, wcstoll);
unsignedlong strtoul(constchar* s,char** end,int base){
returnStrToI<unsignedlong,0, ULONG_MAX,char>(s, end, base);
}
__strong_alias(strtoul_l, strtoul);
unsignedlong wcstoul(constwchar_t* s,wchar_t** end,int base){
returnStrToI<unsignedlong,0, ULONG_MAX,wchar_t>(s, end, base);
}
__strong_alias(wcstoul_l, wcstoul);
unsignedlonglong strtoull(constchar* s,char** end,int base){
returnStrToI<unsignedlonglong,0, ULLONG_MAX,char>(s, end, base);
}
__strong_alias(strtoull_l, strtoull);
unsignedlonglong wcstoull(constwchar_t* s,wchar_t** end,int base){
returnStrToI<unsignedlonglong,0, ULLONG_MAX,wchar_t>(s, end, base);
}
__strong_alias(wcstoull_l, wcstoull);
uintmax_t strtoumax(constchar* s,char** end,int base){
returnStrToI<uintmax_t,0, UINTMAX_MAX,char>(s, end, base);
}
uintmax_t wcstoumax(constwchar_t* s,wchar_t** end,int base){
returnStrToI<uintmax_t,0, UINTMAX_MAX,wchar_t>(s, end, base);
}

[8]ページ先頭

©2009-2025 Movatter.jp