From 1c4151d82e5a6924c3beec5d9cce89c7c4fecc5b Mon Sep 17 00:00:00 2001 From: Rika Ichinose Date: Sat, 8 Apr 2023 07:13:34 +0300 Subject: [PATCH] Remake AdjustLineBreaks. This version is correct and supposedly better in other ways (except for a bit of clarity maybe). --- rtl/objpas/sysutils/sysstr.inc | 115 ++++++++---------- .../test/units/sysutils/tadjustlinebreaks.pp | 72 +++++++++++ 2 files changed, 125 insertions(+), 62 deletions(-) create mode 100644 tests/test/units/sysutils/tadjustlinebreaks.pp diff --git a/rtl/objpas/sysutils/sysstr.inc b/rtl/objpas/sysutils/sysstr.inc index 2ed9af1990..9f62765fea 100644 --- a/rtl/objpas/sysutils/sysstr.inc +++ b/rtl/objpas/sysutils/sysstr.inc @@ -783,69 +783,60 @@ end; function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle): string; var - Source,Dest: PAnsiChar; - DestLen: Integer; - I,J,L: Longint; - + Sp,Se,SLiteralStart,SLiteralEnd,Rp: PChar; begin - Source:=Pointer(S); - L:=Length(S); - DestLen:=L; - I:=1; - while (I<=L) do - begin - case S[i] of - #10: if (Style=tlbsCRLF) then - Inc(DestLen); - #13: if (Style=tlbsCRLF) then - if (I= #32) and (s[i] <= #127) then + result += s[i] + else + result += '#' + IntToStr(ord(s[i])) + + specialize IfThen((i < length(s)) and ((s[i] = #10) or (s[i] = #13) and (pChar(pointer(s))[i] <> #10)), LineEnding, ''); +end; + +procedure TestAdjustLineBreaks(const src: string; style: TTextLineBreakStyle; expect: string); +var + got, styleName: string; +begin + got := AdjustLineBreaks(src, style); + if got <> expect then + begin + WriteStr(styleName, style); + writeln('AdjustLineBreaks(' + LineEnding + + LineEnding + + Repr(src) + ',' + LineEnding + + LineEnding + + styleName + ')' + LineEnding + + LineEnding + + '=' + LineEnding + + LineEnding + + Repr(got) + LineEnding + + LineEnding + + 'expected' + LineEnding + + LineEnding + + Repr(expect) + LineEnding); + somethingFailed := true; + end; +end; + +const + D1 = 'Drinking the soup in the Dining Room will poison Viola and cause her to lose HP with each step.'; + D2 = 'The Chef will chop Viola''s hands off if she chooses to lend the chef a hand in the Kitchen.'; + D3 = 'Upon entering the Spider Room, If Viola takes the Butterfly without placing the Butterfly Model in the web, trying to exit the room will make a spider decapitate her.'; + D4 = 'Reading the Book of Death will cause Viola to violently and uncontrollably bleed to death.'; + D5 = 'Entering the Snake Room without feeding the Frog to the Snake will cause the Snake to eat Viola.'; + D6 = 'If Viola visits the Frog Room after the Frog was killed, and Viola reads the note, a black hand will emerge from the black pit and grab her.'; + LEs: array[TTextLineBreakStyle] of string = (#10, #13#10, #13); + +var + style: TTextLineBreakStyle; + +begin + for style in TTextLineBreakStyle do + begin + TestAdjustLineBreaks( + #10#13 + D1 + #13#10 + D2 + #10 + D3 + #13 + D4 + #13#10#10, style, + LEs[style] + LEs[style] + D1 + LEs[style] + D2 + LEs[style] + D3 + LEs[style] + D4 + LEs[style] + LEs[style]); + + TestAdjustLineBreaks( + D5 + #13 + D6, style, + D5 + LEs[style] + D6); + end; + + if somethingFailed then halt(1); + writeln('ok'); +end.