1

Ошибка в этой части кода:

function ExtractTokens(const line: AnsiString; const sep: AnsiChar; autoUnquote: boolean = true): TStringArray; overload; //overload;
var
  lineIndex: integer;

  function NextChar(out ch: Ansichar): boolean;
  begin
    if lineIndex <= length(line) then
    begin
      result := true;
      ch := line[lineIndex];
      inc(lineIndex);
    end
    else
      result := false;
  end;
  function PeekFor(const ch: AnsiChar): boolean;
  begin
    result := false;
    if lineIndex <= length(line) then
    begin
      if line[lineIndex] = ch then
      begin
        inc(lineIndex);
        result := true;
      end;
    end;
  end;
function UnquoteIfNecessary(const tok: string; quoteChar: char): string;
  var
    pch: PChar;
  begin
    if autoUnquote then
    begin
      pch := pchar(tok);
      result := AnsiExtractQuotedStr(pch, quoteChar);
    end
    else
      result := tok;
  end;
var
  token: string;
  stok: string;
  ch: char;
  lastChar: char;
  strSep: char;
  inString: boolean;

  function IsSep(aChar: char): boolean;
  begin
    result := (aChar = string(sep)) or ((sep = #0) and (ord(aChar) < 33));
  end;
 procedure AddToken(var tokens: TStringArray; const tkn: string; addEmpty: boolean = true);
  var
    s: string;
  begin
    s := trim(tkn);
    if addEmpty or (s <> '') then
    begin
      SetLength(tokens, length(tokens) + 1);
      tokens[high(tokens)] := s;
    end;
    token := '';
  end;
begin
  result := nil;
  token := '';
  stok := '';
  lastChar := #0;
  strSep := #0; // for compiler
  inString := false;
  lineIndex := 1;
  while true do
  begin
    if not NextChar(ch) then
    begin
      AddToken(result, token, (lastChar <> #0) and IsSep(lastChar));
      exit;
    end;

    if ch in ['"', ''''] then
    begin
      stok := stok + ch;
      if inString then
      begin
        if ch = strSep then
        begin
          if PeekFor(AnsiChar(strSep)) then
            stok := stok + strSep
          else
          begin
            token := token + UnquoteIfNecessary(stok, strSep);
            inString := false;
            stok := '';
          end;
        end;
      end
      else
      begin
        strSep := ch;
        inString := true;
      end;
    end
    else if IsSep(ch) and not inString then
      AddToken(result, token, true)
    else
    begin
      if inString then
        stok := stok + ch
      else
        token := token + ch;
    end;
    lastChar := ch;
  end;
end;

В этом месте:

if not NextChar(ch) then

Не пойму как это решить В Delphi 7 все компилировалось, а в 10.2 выдает ошибку

6
  • Как объявлена функция NextChar и какой тип у переменной ch. Вам компилятор говорит, что типы аргумента и параметра не совпадают.
    – zed
    19 сен 2017 в 7:52
  • @zed: Самое интересное я это понимаю, а вот как решить не врубаюсь. Как я уже их не называл!
    – Error404
    19 сен 2017 в 8:13
  • Не вижу объявления функции NextChar и переменной Ch
    – kami
    19 сен 2017 в 8:23
  • @ValertonGT Не видя кода, никто вам не подскажет, как его надо исправить, чтобы он заработал.
    – zed
    19 сен 2017 в 8:24
  • @zed: Я немного добавил кода! Банально не заметил. Библия в 40000 строк кода перевести на 10.2 - мозги немного кругом уже!
    – Error404
    19 сен 2017 в 8:35

1 ответ 1

1

У вас в коде смешаны Ansi и юникодные строки. Вам надо все типы привести к единому, т.е. всюду использовать string и char и не использовать AnsiString.

Внёс буквально пару фиксов и функция скомпилировалась:

function ExtractTokens(const line: String; const sep: Char; autoUnquote: boolean = true): TStringArray; overload; //overload;
var
  lineIndex: integer;

  function NextChar(out ch: Char): boolean;
  begin
    if lineIndex <= length(line) then
    begin
      result := true;
      ch := line[lineIndex];
      inc(lineIndex);
    end
    else
      result := false;
  end;
  function PeekFor(const ch: Char): boolean;
  begin
    result := false;
    if lineIndex <= length(line) then
    begin
      if line[lineIndex] = ch then
      begin
        inc(lineIndex);
        result := true;
      end;
    end;
  end;
function UnquoteIfNecessary(const tok: string; quoteChar: char): string;
  var
    pch: PChar;
  begin
    if autoUnquote then
    begin
      pch := pchar(tok);
      result := AnsiExtractQuotedStr(pch, quoteChar);
    end
    else
      result := tok;
  end;
var
  token: string;
  stok: string;
  ch: char;
  lastChar: char;
  strSep: char;
  inString: boolean;

  function IsSep(aChar: char): boolean;
  begin
    result := (aChar = string(sep)) or ((sep = #0) and (ord(aChar) < 33));
  end;
 procedure AddToken(var tokens: TStringArray; const tkn: string; addEmpty: boolean = true);
  var
    s: string;
  begin
    s := trim(tkn);
    if addEmpty or (s <> '') then
    begin
      SetLength(tokens, length(tokens) + 1);
      tokens[high(tokens)] := s;
    end;
    token := '';
  end;
begin
  result := nil;
  token := '';
  stok := '';
  lastChar := #0;
  strSep := #0; // for compiler
  inString := false;
  lineIndex := 1;
  while true do
  begin
    if not NextChar(ch) then
    begin
      AddToken(result, token, (lastChar <> #0) and IsSep(lastChar));
      exit;
    end;

    if CharInSet(ch, ['"', '''']) then
    begin
      stok := stok + ch;
      if inString then
      begin
        if ch = strSep then
        begin
          if PeekFor(strSep) then
            stok := stok + strSep
          else
          begin
            token := token + UnquoteIfNecessary(stok, strSep);
            inString := false;
            stok := '';
          end;
        end;
      end
      else
      begin
        strSep := ch;
        inString := true;
      end;
    end
    else if IsSep(ch) and not inString then
      AddToken(result, token, true)
    else
    begin
      if inString then
        stok := stok + ch
      else
        token := token + ch;
    end;
    lastChar := ch;
  end;
end;
1
  • Спасибо эта часть кода заработала! Теперь стало еще веселей: E2251 Ambiguous overloaded call to 'StrPas'
    – Error404
    19 сен 2017 в 9:51

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.