mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-18 16:00:16 +02:00
LazUtils: Add a new unit LazStringUtils. Move 2 functions from Codetools' SourceLog there.
git-svn-id: trunk@58631 -
This commit is contained in:
parent
d43fa548b8
commit
5c561fabd8
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -3299,6 +3299,7 @@ components/lazutils/lazloggerdummy.pas svneol=native#text/pascal
|
|||||||
components/lazutils/lazloggerprofiling.pas svneol=native#text/pascal
|
components/lazutils/lazloggerprofiling.pas svneol=native#text/pascal
|
||||||
components/lazutils/lazmethodlist.pas svneol=native#text/pascal
|
components/lazutils/lazmethodlist.pas svneol=native#text/pascal
|
||||||
components/lazutils/lazpasreadutil.pas svneol=native#text/plain
|
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/lazsysutils.pas svneol=native#text/pascal
|
||||||
components/lazutils/laztracer.pas svneol=native#text/pascal
|
components/lazutils/laztracer.pas svneol=native#text/pascal
|
||||||
components/lazutils/lazunicode.pas svneol=native#text/plain
|
components/lazutils/lazunicode.pas svneol=native#text/plain
|
||||||
|
@ -36,7 +36,9 @@ uses
|
|||||||
{$IFDEF MEM_CHECK}
|
{$IFDEF MEM_CHECK}
|
||||||
MemCheck,
|
MemCheck,
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
Classes, SysUtils, LazFileUtils, LazUTF8, lazutf8classes, LazDbgLog;
|
Classes, SysUtils,
|
||||||
|
// LazUtils
|
||||||
|
LazFileUtils, LazUTF8, LazUTF8Classes, LazDbgLog, LazStringUtils;
|
||||||
|
|
||||||
type
|
type
|
||||||
TSourceLog = class;
|
TSourceLog = class;
|
||||||
@ -196,92 +198,8 @@ type
|
|||||||
write FOnEncodeSaving;
|
write FOnEncodeSaving;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function ChangeLineEndings(const s, NewLineEnding: string): string;
|
|
||||||
|
|
||||||
implementation
|
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 }
|
{ TSourceLogEntry }
|
||||||
|
|
||||||
constructor TSourceLogEntry.Create(APos, ALength, AMoveTo: integer;
|
constructor TSourceLogEntry.Create(APos, ALength, AMoveTo: integer;
|
||||||
@ -291,7 +209,7 @@ begin
|
|||||||
Len:=ALength;
|
Len:=ALength;
|
||||||
MoveTo:=AMoveTo;
|
MoveTo:=AMoveTo;
|
||||||
Operation:=AnOperation;
|
Operation:=AnOperation;
|
||||||
LineEnds:=LineEndCount(Txt, LengthOfLastLine);
|
LineEnds:=LineEndingCount(Txt, LengthOfLastLine);
|
||||||
Txt:=ATxt;
|
Txt:=ATxt;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -26,8 +26,11 @@ unit TestBasicCodetools;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
fpcunit, testregistry, contnrs, Classes, SysUtils, FileProcs,
|
fpcunit, testregistry, contnrs, Classes, SysUtils,
|
||||||
BasicCodeTools, SourceLog, DefineTemplates;
|
// LazUtils
|
||||||
|
LazStringUtils,
|
||||||
|
// CodeTools
|
||||||
|
FileProcs, BasicCodeTools, DefineTemplates;
|
||||||
|
|
||||||
type
|
type
|
||||||
{ TTestBasicCodeTools }
|
{ TTestBasicCodeTools }
|
||||||
|
105
components/lazutils/lazstringutils.pas
Normal file
105
components/lazutils/lazstringutils.pas
Normal 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.
|
||||||
|
|
@ -16,7 +16,7 @@
|
|||||||
<Description Value="Useful units for Lazarus packages."/>
|
<Description Value="Useful units for Lazarus packages."/>
|
||||||
<License Value="Modified LGPL-2"/>
|
<License Value="Modified LGPL-2"/>
|
||||||
<Version Major="1"/>
|
<Version Major="1"/>
|
||||||
<Files Count="100">
|
<Files Count="101">
|
||||||
<Item1>
|
<Item1>
|
||||||
<Filename Value="LazLoggerImpl.inc"/>
|
<Filename Value="LazLoggerImpl.inc"/>
|
||||||
<Type Value="Include"/>
|
<Type Value="Include"/>
|
||||||
@ -417,6 +417,10 @@
|
|||||||
<Filename Value="laztracer.pas"/>
|
<Filename Value="laztracer.pas"/>
|
||||||
<UnitName Value="LazTracer"/>
|
<UnitName Value="LazTracer"/>
|
||||||
</Item100>
|
</Item100>
|
||||||
|
<Item101>
|
||||||
|
<Filename Value="lazstringutils.pas"/>
|
||||||
|
<UnitName Value="LazStringUtils"/>
|
||||||
|
</Item101>
|
||||||
</Files>
|
</Files>
|
||||||
<LazDoc Paths="../../docs/xml/lazutils"/>
|
<LazDoc Paths="../../docs/xml/lazutils"/>
|
||||||
<i18n>
|
<i18n>
|
||||||
|
@ -22,7 +22,7 @@ uses
|
|||||||
TTFile, TTGLoad, TTInterp, TTLoad, TTMemory, TTObjs, TTProfile, TTRASTER,
|
TTFile, TTGLoad, TTInterp, TTLoad, TTMemory, TTObjs, TTProfile, TTRASTER,
|
||||||
TTTables, TTTypes, UTF8Process, HTML2TextRender, Laz_AVL_Tree,
|
TTTables, TTTypes, UTF8Process, HTML2TextRender, Laz_AVL_Tree,
|
||||||
CompWriterPas, LazPasReadUtil, IntegerList, LazVersion, UITypes, GraphType,
|
CompWriterPas, LazPasReadUtil, IntegerList, LazVersion, UITypes, GraphType,
|
||||||
LazTracer, LazarusPackageIntf;
|
LazTracer, LazStringUtils, LazarusPackageIntf;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
@ -34,11 +34,11 @@ uses
|
|||||||
Classes, SysUtils, Laz_AVL_Tree,
|
Classes, SysUtils, Laz_AVL_Tree,
|
||||||
// LazUtils
|
// LazUtils
|
||||||
FileUtil, LazFileUtils, LazUtilities, LazFileCache, LazUTF8, LazUTF8Classes,
|
FileUtil, LazFileUtils, LazUtilities, LazFileCache, LazUTF8, LazUTF8Classes,
|
||||||
Laz2_XMLCfg, AvgLvlTree, LazLoggerBase, LazTracer,
|
Laz2_XMLCfg, AvgLvlTree, LazLoggerBase, LazTracer, LazStringUtils,
|
||||||
// LCL
|
// LCL
|
||||||
StdCtrls, ExtCtrls,
|
StdCtrls, ExtCtrls,
|
||||||
// CodeTools
|
// CodeTools
|
||||||
BasicCodeTools, SourceLog, FileProcs, CodeToolManager, CodeToolsConfig, CodeCache,
|
BasicCodeTools, FileProcs, CodeToolManager, CodeToolsConfig, CodeCache,
|
||||||
// IDE
|
// IDE
|
||||||
IDECmdLine, LazConf;
|
IDECmdLine, LazConf;
|
||||||
|
|
||||||
|
@ -53,6 +53,7 @@ uses
|
|||||||
// LazUtils
|
// LazUtils
|
||||||
LConvEncoding, FileUtil, LazFileUtils, LazFileCache, LazUTF8, LazUTF8Classes,
|
LConvEncoding, FileUtil, LazFileUtils, LazFileCache, LazUTF8, LazUTF8Classes,
|
||||||
LazMethodList, LazLoggerBase, LazLogger, Translations, LazUtilities, LazTracer,
|
LazMethodList, LazLoggerBase, LazLogger, Translations, LazUtilities, LazTracer,
|
||||||
|
LazStringUtils,
|
||||||
// codetools
|
// codetools
|
||||||
BasicCodeTools, CodeBeautifier, CodeToolManager, CodeCache, SourceLog,
|
BasicCodeTools, CodeBeautifier, CodeToolManager, CodeCache, SourceLog,
|
||||||
LinkScanner, CodeTree, SourceChanger,
|
LinkScanner, CodeTree, SourceChanger,
|
||||||
|
Loading…
Reference in New Issue
Block a user