简体   繁体   中英

Exception EInvalidPointer because using Unicode chars in array

I'm trying to change chars style by using Unicode chars in Delphi XE8, everything work well except when close application I get Error Exception EInvalidpointer here the code

procedure ChangeStyle(OriginalText, DecoratedText: TsMemo; DecoList: TsListBox);
var
  StyleChars, TempText: string;
  Len, I: Integer;
  TextArray, StyleArray: array of UnicodeString;
begin
  // Put Style chars in array
  StyleChars := DecoList.Items.Strings[DecoList.ItemIndex];
  SetLength(StyleArray, 23);
  for I := 1 to 23 do
    StyleArray[I] := Copy(StyleChars, I, 1);

  // Put originl text in array
  Len := OriginalText.GetTextLen;
  SetLength(TextArray, Len +1);
  for I := 1 to Len do
    TextArray[I] := Copy(OriginalText.Text, I, 1);

  // Set style to chars

  for I := 1 to Len do
  begin
    if (TextArray[I] = 'A') or (TextArray[I] = 'a') then
      TempText := TempText + StyleArray[1]
    else if (TextArray[I] = 'B') or (TextArray[I] = 'b') then
      TempText := TempText + StyleArray[2]
    else if (TextArray[I] = 'C') or (TextArray[I] = 'c') then
      TempText := TempText + StyleArray[3]
    else if (TextArray[I] = 'D') or (TextArray[I] = 'd') then
      TempText := TempText + StyleArray[4]
    else if (TextArray[I] = 'E') or (TextArray[I] = 'e') then
      TempText := TempText + StyleArray[5]
    else if (TextArray[I] = 'F') or (TextArray[I] = 'f') then
      TempText := TempText + StyleArray[6]
    else if (TextArray[I] = 'G') or (TextArray[I] = 'g') then
      TempText := TempText + StyleArray[7]
    else if (TextArray[I] = 'H') or (TextArray[I] = 'h') then
      TempText := TempText + StyleArray[8]
    else if (TextArray[I] = 'I') or (TextArray[I] = 'i') then
      TempText := TempText + StyleArray[9]
    else if (TextArray[I] = 'J') or (TextArray[I] = 'j') then
      TempText := TempText + StyleArray[10]
    else if (TextArray[I] = 'K') or (TextArray[I] = 'k') then
      TempText := TempText + StyleArray[11]
    else if (TextArray[I] = 'L') or (TextArray[I] = 'l') then
      TempText := TempText + StyleArray[12]
    else if (TextArray[I] = 'M') or (TextArray[I] = 'm') then
      TempText := TempText + StyleArray[13]
    else if (TextArray[I] = 'N') or (TextArray[I] = 'n') then
      TempText := TempText + StyleArray[14]
    else if (TextArray[I] = 'O') or (TextArray[I] = 'o') then
      TempText := TempText + StyleArray[15]
    else if (TextArray[I] = 'P') or (TextArray[I] = 'p') then
      TempText := TempText + StyleArray[16]
    else if (TextArray[I] = 'Q') or (TextArray[I] = 'q') then
      TempText := TempText + StyleArray[17]
    else if (TextArray[I] = 'R') or (TextArray[I] = 'r') then
       TempText := TempText + StyleArray[18]
    else if (TextArray[I] = 'S') or (TextArray[I] = 's') then
      TempText := TempText + StyleArray[19]
    else if (TextArray[I] = 'W') or (TextArray[I] = 'w') then
      TempText := TempText + StyleArray[20]
    else if (TextArray[I] = 'X') or (TextArray[I] = 'x') then
      TempText := TempText + StyleArray[21]
    else if (TextArray[I] = 'Y') or (TextArray[I] = 'y') then
      TempText := TempText + StyleArray[22]
    else if (TextArray[I] = 'Z') or (TextArray[I] = 'z') then
      TempText := TempText + StyleArray[23]
    else
      TempText := TempText + TextArray[I];

  end;
  DecoratedText.Text := TempText;
end;

I tried TextArray := nil; and StyleArray := nil but it didn't fix it;
Also tried TextArray, StyleArray: array of String;
When I used TStringList the Exception gone but it doesn't support Unicode
So I think it's something about Arrays or Unicode

SetLength(StyleArray, 23);
for I := 1 to 23 do
  StyleArray[I] := Copy(StyleChars, I, 1);

You write beyond the end of the array here. Dynamic arrays use zero-based indexing. So, valid indices are 0 to 22 inclusive. That explains the runtime exception that you encounter.

Given the one-based indexing everywhere else you probably want to allocate one extra element:

SetLength(StyleArray, 24);

and simply ignore the first element with index 0.

However, I suspect that much of this code can simply be removed, as I explain below.

The huge if statement could be replaced by some arithmetic instead. Note that ord('A') - ord('A') = 0 , ord('B') - ord('A') = 1 , and so on.

The two character arrays seem rather pointless to me. You may as well index directly into the strings. I'd re-write the code by removing those two arrays.

I think I would also avoid using concatenation to populate the string. Allocate it at the start since you know its length ahead of time. The assign to each indexed element directly.

I don't know why you think that TStringList does not support Unicode. It does.

The code you have shown can be greatly simplified:

procedure ChangeStyle(OriginalText, DecoratedText: TsMemo; DecoList: TsListBox);
var
  StyleChars, TempText: string;
  Len, I, Idx: Integer;
  Ch: Char;
begin
  // Put Style chars in array
  StyleChars := DecoList.Items.Strings[DecoList.ItemIndex];

  // make sure the array is big enough
  Assert(Length(StyleChars) >= 23, 'StyleChars must be at least 23 characters');

  // Get original text
  TempText := OriginalText.Text;
  Len := Length(TempText);

  // Set style to chars    
  for I := 1 to Len do
  begin
    Ch := TempText[I];
    case Ch of
      'A'..'S', 'W'..'Z': begin
        // convert uppercase letters A..Z into indexes 0..25
        Idx := Ord(Ch) - Ord('A');
        // no styles for letters T..V, so decrement the indexes for W..Z
        if Idx >= 22 then Dec(Idx, 3);
        // Now replace the character
        TempText[I] := StyleChars[Idx+1];
      end:
      'a'..'s', 'w'..'z': begin
        // convert lowercase letters a..z into indexes 0..25
        Idx := Ord(Ch) - Ord('a');
        // no styles for letters t..v, so decrement the indexes for w..z
        if Idx >= 22 then Dec(Idx, 3);
        // Now replace the character
        TempText[I] := StyleChars[Idx+1];
      end;
    end;
  end;

  DecoratedText.Text := TempText;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM