加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 使用StrUtils’SearchBuf’的奇怪行为

发布时间:2020-12-15 09:19:41 所属栏目:大数据 来源:网络整理
导读:我正在整理过去使用FastStrings的旧代码,并且我已经实现了我的’PosAnyCase’的旧例程,它应该像’Pos’一样操作. (我希望SearchBuf比在两个字符串上调用UpperCase更好). function PosAnyCase( const AFindStr,AStr : string ) : integer;// Returns the posi
我正在整理过去使用FastStrings的旧代码,并且我已经实现了我的’PosAnyCase’的旧例程,它应该像’Pos’一样操作. (我希望SearchBuf比在两个字符串上调用UpperCase更好).

function PosAnyCase( const AFindStr,AStr : string ) : integer;
// Returns the position of this substring within a string ignoring case

我正在使用SearchBuf如下:

function PosAnyCase( const AFindStr,AStr : string ) : integer;
// Returns the position of this substring within a string ignoring case
var
  Start,ResultPos : PChar;
begin
  Start := PChar( AStr );

  ResultPos := SearchBuf(
    Start,ByteLength( AStr ),AFindStr,[soDown] );

  if ResultPos = nil then
    Result := 0
   else
    Result := ResultPos-Start+1;
end;

当我从单元测试中调用此例程时,以下测试通过:

Check(
    PosAnyCase( '','123' ) = 0 );
  Check(
    PosAnyCase( '2','123' ) = 2 );
  Check(
    PosAnyCase( 'A','ABC' ) = 1 );
  Check(
    PosAnyCase( 'a','ABC' ) = 1 );
  Check(
    PosAnyCase( 'the','hellot there' ) = 8 );
  Check(
    PosAnyCase( 'THE','hellot there' ) = 8 );

但是这个测试失败了:

Check(
    PosAnyCase( 'nice','does not have n i c e' ) = 0 );

我做错了什么? SearchBuf上的文档非常有限….
谢谢

解决方法

对ByteLength的调用不正确.虽然 documentation明确声明参数是以字节为单位的长度,但事实并非如此.你应该使用Length代替,因为函数实际上需要char的单位而不是byte的单位.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读