似乎StrToInt没有Ansi重载.这是正确的吗?或许我错过了一些东西.StrToInt坚持将我的ansistrings转换为字符串.
你是对的.没有ANSI版本StrToInt.找到标准功能的ANSI版本的地方就是AnsiStrings单位,那里什么也没有.
编写自己的函数来完成工作,或接受使用所需的转换StrToInt.
编写自己的函数并不难.它可能看起来像这样:
uses
SysConst; // for SInvalidInteger
....
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
function AnsiStrToInt(const s: AnsiString): Integer;
procedure Error;
begin
raise EConvertError.CreateResFmt(@SInvalidInteger, [s]);
end;
var
Index, Len, Digit: Integer;
Negative: Boolean;
begin
Index := 1;
Result := 0;
Negative := False;
Len := Length(s);
while (Index <= Len) and (s[Index] = ' ') do
inc(Index);
if Index > Len then
Error;
case s[Index] of
'-','+':
begin
Negative := s[Index] = '-';
inc(Index);
if Index > Len then
Error;
end;
end;
while Index <= Len do
begin
Digit := ord(s[Index]) - ord('0');
if (Digit < 0) or (Digit > 9) then
Error;
Result := Result * 10 + Digit;
if Result < 0 then
Error;
inc(Index);
end;
if Negative then
Result := -Result;
end;
Run Code Online (Sandbox Code Playgroud)
这是发现的缩减版本StrToInt.它不处理十六进制,并且在错误方面更严格一些.在使用此代码之前,我想测试这是否真的是您的瓶颈.
非常有趣的是,此代码基于RTL源中的代码,无法返回low(Integer).解决这个问题并不难,但这会使代码更加复杂.
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |