LazUtils: Add a new unit LazStringUtils. Move 2 functions from Codetools' SourceLog there.

git-svn-id: trunk@58631 -
This commit is contained in:
juha 2018-07-25 13:39:01 +00:00
parent d43fa548b8
commit 5c561fabd8
8 changed files with 124 additions and 92 deletions

1
.gitattributes vendored
View File

@ -3299,6 +3299,7 @@ components/lazutils/lazloggerdummy.pas svneol=native#text/pascal
components/lazutils/lazloggerprofiling.pas svneol=native#text/pascal
components/lazutils/lazmethodlist.pas svneol=native#text/pascal
components/lazutils/lazpasreadutil.pas svneol=native#text/plain
components/lazutils/lazstringutils.pas svneol=native#text/pascal
components/lazutils/lazsysutils.pas svneol=native#text/pascal
components/lazutils/laztracer.pas svneol=native#text/pascal
components/lazutils/lazunicode.pas svneol=native#text/plain

View File

@ -36,7 +36,9 @@ uses
{$IFDEF MEM_CHECK}
MemCheck,
{$ENDIF}
Classes, SysUtils, LazFileUtils, LazUTF8, lazutf8classes, LazDbgLog;
Classes, SysUtils,
// LazUtils
LazFileUtils, LazUTF8, LazUTF8Classes, LazDbgLog, LazStringUtils;
type
TSourceLog = class;
@ -196,92 +198,8 @@ type
write FOnEncodeSaving;
end;
function ChangeLineEndings(const s, NewLineEnding: string): string;
implementation
{ useful function }
function LineEndCount(const Txt: string;var LengthOfLastLine: integer): integer;
var i, LastLineEndPos, TxtLen: integer;
begin
i:=1;
LastLineEndPos:=0;
Result:=0;
TxtLen:=length(Txt);
while i<TxtLen do begin
if (Txt[i] in [#10,#13]) then begin
inc(Result);
inc(i);
if (i<=TxtLen) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then
inc(i);
LastLineEndPos:=i-1;
end else
inc(i);
end;
LengthOfLastLine:=TxtLen-LastLineEndPos;
end;
function ChangeLineEndings(const s, NewLineEnding: string): string;
var
NewLength: Integer;
p: Integer;
Src, Dest, EndPos: PChar;
EndLen: Integer;
begin
if s='' then begin
Result:=s;
exit;
end;
EndLen:=length(NewLineEnding);
NewLength:=length(s);
Src:=PChar(s);
repeat
case Src^ of
#0:
if Src-PChar(s)=length(s) then
break
else
inc(Src);
#10,#13:
begin
if (Src[1] in [#10,#13]) and (Src^<>Src[1]) then begin
inc(Src,2);
inc(NewLength,EndLen-2);
end else begin
inc(Src);
inc(NewLength,EndLen-1);
end;
end;
else
inc(Src);
end;
until false;
SetLength(Result,NewLength);
Src:=PChar(s);
Dest:=PChar(Result);
EndPos:=Dest+NewLength;
while (Dest<EndPos) do begin
if Src^ in [#10,#13] then begin
for p:=1 to EndLen do begin
Dest^:=NewLineEnding[p];
inc(Dest);
end;
if (Src[1] in [#10,#13]) and (Src^<>Src[1]) then
inc(Src,2)
else
inc(Src);
end else begin
Dest^:=Src^;
inc(Src);
inc(Dest);
end;
end;
//if Src-1<>@s[length(s)] then RaiseGDBException('');
end;
{ TSourceLogEntry }
constructor TSourceLogEntry.Create(APos, ALength, AMoveTo: integer;
@ -291,7 +209,7 @@ begin
Len:=ALength;
MoveTo:=AMoveTo;
Operation:=AnOperation;
LineEnds:=LineEndCount(Txt, LengthOfLastLine);
LineEnds:=LineEndingCount(Txt, LengthOfLastLine);
Txt:=ATxt;
end;

View File

@ -26,8 +26,11 @@ unit TestBasicCodetools;
interface
uses
fpcunit, testregistry, contnrs, Classes, SysUtils, FileProcs,
BasicCodeTools, SourceLog, DefineTemplates;
fpcunit, testregistry, contnrs, Classes, SysUtils,
// LazUtils
LazStringUtils,
// CodeTools
FileProcs, BasicCodeTools, DefineTemplates;
type
{ TTestBasicCodeTools }

View File

@ -0,0 +1,105 @@
{
*****************************************************************************
This file is part of LazUtils.
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Functions for string manipulation.
}
unit LazStringUtils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
function LineEndingCount(const Txt: string; var LengthOfLastLine: integer): integer;
function ChangeLineEndings(const s, NewLineEnding: string): string;
implementation
function LineEndingCount(const Txt: string; var LengthOfLastLine: integer): integer;
var
i, LastLineEndPos, TxtLen: integer;
begin
i:=1;
LastLineEndPos:=0;
Result:=0;
TxtLen:=length(Txt);
while i<TxtLen do begin
if (Txt[i] in [#10,#13]) then begin
inc(Result);
inc(i);
if (i<=TxtLen) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then
inc(i);
LastLineEndPos:=i-1;
end else
inc(i);
end;
LengthOfLastLine:=TxtLen-LastLineEndPos;
end;
function ChangeLineEndings(const s, NewLineEnding: string): string;
var
p, NewLength, EndLen: Integer;
Src, Dest, EndPos: PChar;
begin
if s='' then begin
Result:=s;
exit;
end;
EndLen:=length(NewLineEnding);
NewLength:=length(s);
Src:=PChar(s);
repeat
case Src^ of
#0:
if Src-PChar(s)=length(s) then
break
else
inc(Src);
#10,#13:
begin
if (Src[1] in [#10,#13]) and (Src^<>Src[1]) then begin
inc(Src,2);
inc(NewLength,EndLen-2);
end else begin
inc(Src);
inc(NewLength,EndLen-1);
end;
end;
else
inc(Src);
end;
until false;
SetLength(Result,NewLength);
Src:=PChar(s);
Dest:=PChar(Result);
EndPos:=Dest+NewLength;
while (Dest<EndPos) do begin
if Src^ in [#10,#13] then begin
for p:=1 to EndLen do begin
Dest^:=NewLineEnding[p];
inc(Dest);
end;
if (Src[1] in [#10,#13]) and (Src^<>Src[1]) then
inc(Src,2)
else
inc(Src);
end else begin
Dest^:=Src^;
inc(Src);
inc(Dest);
end;
end;
//if Src-1<>@s[length(s)] then RaiseGDBException('');
end;
end.

View File

@ -16,7 +16,7 @@
<Description Value="Useful units for Lazarus packages."/>
<License Value="Modified LGPL-2"/>
<Version Major="1"/>
<Files Count="100">
<Files Count="101">
<Item1>
<Filename Value="LazLoggerImpl.inc"/>
<Type Value="Include"/>
@ -417,6 +417,10 @@
<Filename Value="laztracer.pas"/>
<UnitName Value="LazTracer"/>
</Item100>
<Item101>
<Filename Value="lazstringutils.pas"/>
<UnitName Value="LazStringUtils"/>
</Item101>
</Files>
<LazDoc Paths="../../docs/xml/lazutils"/>
<i18n>

View File

@ -22,7 +22,7 @@ uses
TTFile, TTGLoad, TTInterp, TTLoad, TTMemory, TTObjs, TTProfile, TTRASTER,
TTTables, TTTypes, UTF8Process, HTML2TextRender, Laz_AVL_Tree,
CompWriterPas, LazPasReadUtil, IntegerList, LazVersion, UITypes, GraphType,
LazTracer, LazarusPackageIntf;
LazTracer, LazStringUtils, LazarusPackageIntf;
implementation

View File

@ -34,11 +34,11 @@ uses
Classes, SysUtils, Laz_AVL_Tree,
// LazUtils
FileUtil, LazFileUtils, LazUtilities, LazFileCache, LazUTF8, LazUTF8Classes,
Laz2_XMLCfg, AvgLvlTree, LazLoggerBase, LazTracer,
Laz2_XMLCfg, AvgLvlTree, LazLoggerBase, LazTracer, LazStringUtils,
// LCL
StdCtrls, ExtCtrls,
// CodeTools
BasicCodeTools, SourceLog, FileProcs, CodeToolManager, CodeToolsConfig, CodeCache,
BasicCodeTools, FileProcs, CodeToolManager, CodeToolsConfig, CodeCache,
// IDE
IDECmdLine, LazConf;

View File

@ -53,6 +53,7 @@ uses
// LazUtils
LConvEncoding, FileUtil, LazFileUtils, LazFileCache, LazUTF8, LazUTF8Classes,
LazMethodList, LazLoggerBase, LazLogger, Translations, LazUtilities, LazTracer,
LazStringUtils,
// codetools
BasicCodeTools, CodeBeautifier, CodeToolManager, CodeCache, SourceLog,
LinkScanner, CodeTree, SourceChanger,