--- Merging r41271 into '.':

U    packages/fpmake_add.inc
U    packages/fpmake_proc.inc
A    packages/libmagic
A    packages/libmagic/Makefile
A    packages/libmagic/Makefile.fpc
A    packages/libmagic/examples
A    packages/libmagic/examples/basic.pp
A    packages/libmagic/fpmake.pp
A    packages/libmagic/src
A    packages/libmagic/src/libmagic.pp
--- Recording mergeinfo for merge of r41271 into '.':
 U   .
--- Merging r41330 into '.':
U    rtl/objpas/sysutils/syssr.inc
U    rtl/objpas/sysutils/sysstr.inc
U    rtl/objpas/sysutils/sysstrh.inc
U    rtl/objpas/sysutils/sysuni.inc
U    rtl/objpas/sysutils/sysunih.inc
U    rtl/objpas/sysutils/syswide.inc
U    rtl/objpas/sysutils/syswideh.inc
--- Recording mergeinfo for merge of r41330 into '.':
 G   .
--- Merging r41336 into '.':
G    rtl/objpas/sysutils/sysstr.inc
G    rtl/objpas/sysutils/sysstrh.inc
U    rtl/win/sysutils.pp
--- Recording mergeinfo for merge of r41336 into '.':
 G   .
--- Merging r41337 into '.':
G    rtl/win/sysutils.pp
--- Recording mergeinfo for merge of r41337 into '.':
 G   .
--- Merging r41342 into '.':
U    rtl/objpas/objpas.pp
--- Recording mergeinfo for merge of r41342 into '.':
 G   .
--- Merging r41343 into '.':
U    packages/rtl-objpas/src/inc/strutils.pp
--- Recording mergeinfo for merge of r41343 into '.':
 G   .
--- Recording mergeinfo for merge of r41400 into '.':
 G   .
--- Merging r41547 into '.':
U    rtl/objpas/sysutils/sysencodingh.inc
--- Recording mergeinfo for merge of r41547 into '.':
 G   .

# revisions: 41271,41330,41336,41337,41342,41343,41400,41547
r41271 | michael | 2019-02-09 20:57:11 +0100 (Sat, 09 Feb 2019) | 1 line
Changed paths:
   M /trunk/packages/fpmake_add.inc
   M /trunk/packages/fpmake_proc.inc
   A /trunk/packages/libmagic
   A /trunk/packages/libmagic/Makefile
   A /trunk/packages/libmagic/Makefile.fpc
   A /trunk/packages/libmagic/examples
   A /trunk/packages/libmagic/examples/basic.pp
   A /trunk/packages/libmagic/fpmake.pp
   A /trunk/packages/libmagic/src
   A /trunk/packages/libmagic/src/libmagic.pp

* libmagic headers by Silvio Clecio
r41330 | michael | 2019-02-16 08:55:26 +0100 (Sat, 16 Feb 2019) | 1 line
Changed paths:
   M /trunk/rtl/objpas/sysutils/syssr.inc
   M /trunk/rtl/objpas/sysutils/sysstr.inc
   M /trunk/rtl/objpas/sysutils/sysstrh.inc
   M /trunk/rtl/objpas/sysutils/sysuni.inc
   M /trunk/rtl/objpas/sysutils/sysunih.inc
   M /trunk/rtl/objpas/sysutils/syswide.inc
   M /trunk/rtl/objpas/sysutils/syswideh.inc

* Add overloaded version of stringreplace with replacement count (ID 23987)
r41336 | michael | 2019-02-16 11:50:31 +0100 (Sat, 16 Feb 2019) | 1 line
Changed paths:
   M /trunk/rtl/objpas/sysutils/sysstr.inc
   M /trunk/rtl/objpas/sysutils/sysstrh.inc
   M /trunk/rtl/win/sysutils.pp

* Complete the implementation of IsLeadChar
r41337 | michael | 2019-02-16 11:52:51 +0100 (Sat, 16 Feb 2019) | 1 line
Changed paths:
   M /trunk/rtl/win/sysutils.pp

* Actually call InitLeadBytes
r41342 | michael | 2019-02-16 13:52:00 +0100 (Sat, 16 Feb 2019) | 1 line
Changed paths:
   M /trunk/rtl/objpas/objpas.pp

* Add overloaded version of AssignFile with codepage. See bug ID #34889
r41343 | michael | 2019-02-16 15:52:17 +0100 (Sat, 16 Feb 2019) | 1 line
Changed paths:
   M /trunk/packages/rtl-objpas/src/inc/strutils.pp

* Fix bug ID #35098 (count param)
r41400 | yury | 2019-02-20 16:11:45 +0100 (Wed, 20 Feb 2019) | 1 line
Changed paths:
   M /trunk/rtl/android/sysandroid.inc
   M /trunk/rtl/linux/system.pp

* android: Fixed stdio redirection for threads.
r41547 | michael | 2019-03-02 11:42:19 +0100 (Sat, 02 Mar 2019) | 1 line
Changed paths:
   M /trunk/rtl/objpas/sysutils/sysencodingh.inc

* Make FreeEncodings protected: they cannot be public, as it causes AVs (see bug ID #34856)

git-svn-id: branches/fixes_3_2@41933 -
This commit is contained in:
marco 2019-04-25 12:40:02 +00:00
parent 636246e095
commit e41ddf221c
19 changed files with 3036 additions and 17 deletions

5
.gitattributes vendored
View File

@ -5363,6 +5363,11 @@ packages/libgd/examples/gdtest.pp svneol=native#text/plain
packages/libgd/examples/gdtestcgi.pp svneol=native#text/plain
packages/libgd/fpmake.pp svneol=native#text/plain
packages/libgd/src/gd.pas svneol=native#text/plain
packages/libmagic/Makefile svneol=native#text/plain
packages/libmagic/Makefile.fpc svneol=native#text/plain
packages/libmagic/examples/basic.pp svneol=native#text/plain
packages/libmagic/fpmake.pp svneol=native#text/plain
packages/libmagic/src/libmagic.pp svneol=native#text/plain
packages/libmicrohttpd/Makefile svneol=native#text/plain
packages/libmicrohttpd/Makefile.fpc svneol=native#text/plain
packages/libmicrohttpd/examples/basicauthentication.pp svneol=native#text/plain

View File

@ -11,6 +11,7 @@
add_cocoaint(ADirectory+IncludeTrailingPathDelimiter('cocoaint'));
add_dblib(ADirectory+IncludeTrailingPathDelimiter('dblib'));
add_dbus(ADirectory+IncludeTrailingPathDelimiter('dbus'));
add_libmagic(ADirectory+IncludeTrailingPathDelimiter('libmagic'));
add_dts(ADirectory+IncludeTrailingPathDelimiter('dts'));
add_fastcgi(ADirectory+IncludeTrailingPathDelimiter('fastcgi'));
add_fcl_async(ADirectory+IncludeTrailingPathDelimiter('fcl-async'));

View File

@ -72,6 +72,12 @@ begin
{$include dbus/fpmake.pp}
end;
procedure add_libmagic(const ADirectory: string);
begin
with Installer do
{$include libmagic/fpmake.pp}
end;
procedure add_dts(const ADirectory: string);
begin
with Installer do

2553
packages/libmagic/Makefile Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,102 @@
#
# Makefile.fpc for running fpmake
#
[package]
name=libmagiv
version=3.3.1
[require]
packages=rtl fpmkunit
[install]
fpcpackage=y
[default]
fpcdir=../..
[prerules]
FPMAKE_BIN_CLEAN=$(wildcard ./fpmake$(SRCEXEEXT))
ifdef OS_TARGET
FPC_TARGETOPT+=--os=$(OS_TARGET)
endif
ifdef CPU_TARGET
FPC_TARGETOPT+=--cpu=$(CPU_TARGET)
endif
LOCALFPMAKE=./fpmake$(SRCEXEEXT)
[rules]
# Do not pass the Makefile's unit and binary target locations. Fpmake uses it's own.
override FPCOPT:=$(filter-out -FU%,$(FPCOPT))
override FPCOPT:=$(filter-out -FE%,$(FPCOPT))
# Do not pass the package-unitdirectories. Fpmake adds those and this way they don't apear in the .fpm
override FPCOPT:=$(filter-out $(addprefix -Fu,$(COMPILER_UNITDIR)),$(FPCOPT))# Compose general fpmake-parameters
# Compose general fpmake-parameters
ifdef FPMAKEOPT
FPMAKE_OPT+=$(FPMAKEOPT)
endif
FPMAKE_OPT+=--localunitdir=../..
FPMAKE_OPT+=--globalunitdir=..
FPMAKE_OPT+=$(FPC_TARGETOPT)
FPMAKE_OPT+=$(addprefix -o ,$(FPCOPT))
FPMAKE_OPT+=--compiler=$(FPC)
FPMAKE_OPT+=-bu
.NOTPARALLEL:
fpmake$(SRCEXEEXT): fpmake.pp
$(FPCFPMAKE) fpmake.pp $(FPMAKE_SKIP_CONFIG) $(addprefix -Fu,$(COMPILER_FPMAKE_UNITDIR)) $(FPCMAKEOPT) $(OPT)
all: fpmake$(SRCEXEEXT)
$(LOCALFPMAKE) compile $(FPMAKE_OPT)
smart: fpmake$(SRCEXEEXT)
$(LOCALFPMAKE) compile $(FPMAKE_OPT) -o -XX -o -CX
release: fpmake$(SRCEXEEXT)
$(LOCALFPMAKE) compile $(FPMAKE_OPT) -o -dRELEASE
debug: fpmake$(SRCEXEEXT)
$(LOCALFPMAKE) compile $(FPMAKE_OPT) -o -dDEBUG
# If no fpmake exists and (dist)clean is called, do not try to build fpmake, it will
# most often fail because the dependencies are cleared.
# In case of a clean, simply do nothing
ifeq ($(FPMAKE_BIN_CLEAN),)
clean:
else
clean:
$(FPMAKE_BIN_CLEAN) clean $(FPMAKE_OPT)
endif
# In case of a distclean, perform an 'old'-style distclean. This to avoid problems
# when the package is compiled using fpcmake prior to running this clean using fpmake
ifeq ($(FPMAKE_BIN_CLEAN),)
distclean: $(addsuffix _distclean,$(TARGET_DIRS)) fpc_cleanall
else
distclean:
ifdef inUnix
{ $(FPMAKE_BIN_CLEAN) distclean $(FPMAKE_OPT); if [ $$? != "0" ]; then { echo Something wrong with fpmake exectable. Remove the executable and call make recursively to recover.; $(DEL) $(FPMAKE_BIN_CLEAN); $(MAKE) fpc_cleanall; }; fi; }
else
$(FPMAKE_BIN_CLEAN) distclean $(FPMAKE_OPT)
endif
-$(DEL) $(LOCALFPMAKE)
endif
cleanall: distclean
install: fpmake$(SRCEXEEXT)
ifdef UNIXHier
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_PREFIX) --baseinstalldir=$(INSTALL_LIBDIR)/fpc/$(FPC_VERSION) --unitinstalldir=$(INSTALL_UNITDIR)
else
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_BASEDIR) --baseinstalldir=$(INSTALL_BASEDIR) --unitinstalldir=$(INSTALL_UNITDIR)
endif
# distinstall also installs the example-sources and omits the location of the source-
# files from the fpunits.cfg files.
distinstall: fpmake$(SRCEXEEXT)
ifdef UNIXHier
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_PREFIX) --baseinstalldir=$(INSTALL_LIBDIR)/fpc/$(FPC_VERSION) --unitinstalldir=$(INSTALL_UNITDIR) -ie -fsp 0
else
$(LOCALFPMAKE) install $(FPMAKE_OPT) --prefix=$(INSTALL_BASEDIR) --baseinstalldir=$(INSTALL_BASEDIR) --unitinstalldir=$(INSTALL_UNITDIR) -ie -fsp 0
endif
zipinstall: fpmake$(SRCEXEEXT)
$(LOCALFPMAKE) zipinstall $(FPMAKE_OPT) --zipprefix=$(DIST_DESTDIR)/$(ZIPPREFIX)
zipdistinstall: fpmake$(SRCEXEEXT)
$(LOCALFPMAKE) zipinstall $(FPMAKE_OPT) --zipprefix=$(DIST_DESTDIR)/$(ZIPPREFIX) -ie -fsp 0
zipsourceinstall: fpmake$(SRCEXEEXT)
ifdef UNIXHier
$(LOCALFPMAKE) archive $(FPMAKE_OPT) --zipprefix=$(DIST_DESTDIR)/$(ZIPPREFIX) --prefix=share/src/fpc-\$$\(PACKAGEVERSION\)/$(INSTALL_FPCSUBDIR)/\$$\(PACKAGEDIRECTORY\)
else
$(LOCALFPMAKE) archive $(FPMAKE_OPT) --zipprefix=$(DIST_DESTDIR)/$(ZIPPREFIX) --prefix=source\\$(INSTALL_FPCSUBDIR)\\\$$\(PACKAGEDIRECTORY\)
endif

View File

@ -0,0 +1,31 @@
program basic;
{$IFDEF FPC}
{$MODE OBJFPC}
{$H+}
{$ENDIF}
{$IFDEF MSWINDOWS}
{$APPTYPE CONSOLE}
{$ENDIF}
uses libmagic;
const
MAGIC_MGC = {$IFDEF MSWINFOWS}'magic.mgc'{$ELSE}nil{$ENDIF};
var
filename: string;
cookie: magic_t;
begin
cookie := magic_open(MAGIC_ERROR_ or MAGIC_MIME);
magic_load(cookie, MAGIC_MGC);
filename := {$I %file%};
WriteLn('The content-type of ''', filename, ''' is: ',
magic_file(cookie, Pcchar(filename)));
magic_close(cookie);
{$IFDEF MSWINDOWS}
WriteLn('Press [ENTER] to exit ...');
ReadLn;
{$ENDIF}
end.

View File

@ -0,0 +1,40 @@
{$ifndef ALLPACKAGES}
{$mode objfpc}{$H+}
program fpmake;
uses fpmkunit;
Var
P : TPackage;
T : TTarget;
begin
With Installer do
begin
{$endif ALLPACKAGES}
P:=AddPackage('libmagic');
P.ShortName:='magic';
{$ifdef ALLPACKAGES}
P.Directory:=ADirectory;
{$endif ALLPACKAGES}
P.Version:='3.3.1';
P.Author := 'Library: libmagic';
P.License := 'Library: GPL, header: LGPL with modification, ';
P.HomepageURL := 'www.freepascal.org';
P.Email := '';
P.Description := 'Headers for the magic library (library to determine file type)';
P.NeedLibC:= true; // true for headers that indirectly link to libc?
P.OSes := AllUnixOSes-[qnx];
P.SourcePath.Add('src');
P.IncludePath.Add('src');
T:=P.Targets.AddUnit('libmagic.pp');
P.ExamplePath.Add('examples');
P.Targets.AddExampleProgram('basic.pp');
{$ifndef ALLPACKAGES}
Run;
end;
end.
{$endif ALLPACKAGES}

View File

@ -0,0 +1,160 @@
{
This file is part of the Free Pascal packages
Copyright (C) 2019 Silvio Clecio (silvioprog)
Pascal binding for libmagic(3)
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit libmagic;
{$IFDEF FPC}
{$MODE OBJFPC}
{$H+}
{$ENDIF}
interface
type
Pcchar = PAnsiChar;
cint = LongInt;
csize_t = NativeUInt;
Pcsize_t = PNativeUInt;
Pcvoid = Pointer;
PPcvoid = PPointer;
const
MAGIC_LIB_NAME = {$IFDEF MSWINDOWS}'libmagic-1'{$ELSE}'magic'{$ENDIF};
const
MAGIC_NONE = $0000000; // No flags
MAGIC_DEBUG = $0000001; // Turn on debugging
MAGIC_SYMLINK = $0000002; // Follow symlinks
MAGIC_COMPRESS = $0000004; // Check inside compressed files
MAGIC_DEVICES = $0000008; // Look at the contents of devices
MAGIC_MIME_TYPE = $0000010; // Return the MIME type
MAGIC_CONTINUE = $0000020; // Return all matches
MAGIC_CHECK_ = $0000040; // Print warnings to stderr
MAGIC_PRESERVE_ATIME = $0000080; // Restore access time on exit
MAGIC_RAW = $0000100; // Don't convert unprintable chars
MAGIC_ERROR_ = $0000200; // Handle ENOENT etc as real errors
MAGIC_MIME_ENCODING = $0000400; // Return the MIME encoding
MAGIC_MIME = MAGIC_MIME_TYPE or MAGIC_MIME_ENCODING;
MAGIC_APPLE = $0000800; // Return the Apple creator/type
MAGIC_EXTENSION = $1000000; // Return a /-separated list of extensions
MAGIC_COMPRESS_TRANSP = $2000000; // Check inside compressed files but not report compression
MAGIC_NODESC = MAGIC_EXTENSION or MAGIC_MIME or MAGIC_APPLE;
const
MAGIC_NO_CHECK_COMPRESS = $0001000; // Don't check for compressed files
MAGIC_NO_CHECK_TAR = $0002000; // Don't check for tar files
MAGIC_NO_CHECK_SOFT = $0004000; // Don't check magic entries
MAGIC_NO_CHECK_APPTYPE = $0008000; // Don't check application type
MAGIC_NO_CHECK_ELF = $0010000; // Don't check for elf details
MAGIC_NO_CHECK_TEXT = $0020000; // Don't check for text files
MAGIC_NO_CHECK_CDF = $0040000; // Don't check for cdf files
MAGIC_NO_CHECK_TOKENS = $0100000; // Don't check tokens
MAGIC_NO_CHECK_ENCODING = $0200000; // Don't check text encodings
const
// No built-in tests; only consult the magic file
MAGIC_NO_CHECK_BUILTIN =
MAGIC_NO_CHECK_COMPRESS or
MAGIC_NO_CHECK_TAR or
//MAGIC_NO_CHECK_SOFT or
MAGIC_NO_CHECK_APPTYPE or
MAGIC_NO_CHECK_ELF or
MAGIC_NO_CHECK_TEXT or
MAGIC_NO_CHECK_CDF or
MAGIC_NO_CHECK_TOKENS or
MAGIC_NO_CHECK_ENCODING or
0;
const
MAGIC_SNPRINTB = #177#020+
'b'#0'debug'#0+
'b'#1'symlink'#0+
'b'#2'compress'#0+
'b'#3'devices'#0+
'b'#4'mime_type'#0+
'b'#5'continue'#0+
'b'#6'check'#0+
'b'#7'preserve_atime'#0+
'b'#10'raw'#0+
'b'#11'error'#0+
'b'#12'mime_encoding'#0+
'b'#13'apple'#0+
'b'#14'no_check_compress'#0+
'b'#15'no_check_tar'#0+
'b'#16'no_check_soft'#0+
'b'#17'no_check_sapptype'#0+
'b'#20'no_check_elf'#0+
'b'#21'no_check_text'#0+
'b'#22'no_check_cdf'#0+
'b'#23'no_check_reserved0'#0+
'b'#24'no_check_tokens'#0+
'b'#25'no_check_encoding'#0+
'b'#26'no_check_reserved1'#0+
'b'#27'no_check_reserved2'#0+
'b'#30'extension'#0+
'b'#31'transp_compression'#0;
const
// Defined for backwards compatibility (renamed)
MAGIC_NO_CHECK_ASCII = MAGIC_NO_CHECK_TEXT;
const
// Defined for backwards compatibility; do nothing
MAGIC_NO_CHECK_FORTRAN = $000000; // Don't check ascii/fortran
MAGIC_NO_CHECK_TROFF = $000000; // Don't check ascii/troff
const
MAGIC_VERSION_ = 532; // This implementation
type
magic_t = ^magic_set;
magic_set = record
end;
function magic_open(flags: cint): magic_t; cdecl; external MAGIC_LIB_NAME name 'magic_open';
procedure magic_close(cookie: magic_t); cdecl; external MAGIC_LIB_NAME name 'magic_close';
function magic_getpath(const magicfile: Pcchar; action: cint): Pcchar; cdecl; external MAGIC_LIB_NAME name 'magic_getpath';
function magic_file(cookie: magic_t; const filename: Pcchar): Pcchar; cdecl; external MAGIC_LIB_NAME name 'magic_file';
function magic_descriptor(cookie: magic_t; fd: cint): Pcchar; cdecl; external MAGIC_LIB_NAME name 'magic_descriptor';
function magic_buffer(cookie: magic_t; const buffer: Pcvoid; length: csize_t): Pcchar; cdecl; external MAGIC_LIB_NAME name 'magic_buffer';
function magic_error(cookie: magic_t): Pcchar; cdecl; external MAGIC_LIB_NAME name 'magic_error';
function magic_getflags(cookie: magic_t): cint; cdecl; external MAGIC_LIB_NAME name 'magic_getflags';
function magic_setflags(cookie: magic_t; flags: cint): cint; cdecl; external MAGIC_LIB_NAME name 'magic_setflags';
function magic_version: cint; cdecl; external MAGIC_LIB_NAME name 'magic_version';
function magic_load(cookie: magic_t; const filename: Pcchar): cint; cdecl; external MAGIC_LIB_NAME name 'magic_load';
function magic_load_buffers(cookie: magic_t; buffers: PPcvoid; sizes: Pcsize_t; nbuffers: csize_t): cint; cdecl; external MAGIC_LIB_NAME name 'magic_load_buffers';
function magic_compile(cookie: magic_t; const filename: Pcchar): cint; cdecl; external MAGIC_LIB_NAME name 'magic_compile';
function magic_check(cookie: magic_t; const filename: Pcchar): cint; cdecl; external MAGIC_LIB_NAME name 'magic_check';
function magic_list(cookie: magic_t; const filename: Pcchar): cint; cdecl; external MAGIC_LIB_NAME name 'magic_list';
function magic_errno(cookie: magic_t): cint; cdecl; external MAGIC_LIB_NAME name 'magic_errno';
const
MAGIC_PARAM_INDIR_MAX = 0;
MAGIC_PARAM_NAME_MAX = 1;
MAGIC_PARAM_ELF_PHNUM_MAX = 2;
MAGIC_PARAM_ELF_SHNUM_MAX = 3;
MAGIC_PARAM_ELF_NOTES_MAX = 4;
MAGIC_PARAM_REGEX_MAX = 5;
MAGIC_PARAM_BYTES_MAX = 6;
function magic_setparam(cookie: magic_t; param: cint; const value: Pcvoid): cint; cdecl; external MAGIC_LIB_NAME name 'magic_setparam';
function magic_getparam(cookie: magic_t; param: cint; value: Pcvoid): cint; cdecl; external MAGIC_LIB_NAME name 'magic_getparam';
implementation
end.

View File

@ -258,6 +258,7 @@ Type
sraBoyerMoore // Algorithm optimized for long replacements.
);
Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; out aCount : Integer; Algorithm : TStringReplaceAlgorithm = sraDefault): string; overload;
Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Algorithm : TStringReplaceAlgorithm = sraDefault): string; overload;
{ We need these for backwards compatibility:
The compiler will stop searching and convert to ansistring if the widestring version of stringreplace is used.
@ -576,8 +577,7 @@ begin
Result:=MatchesCount>0;
end;
function StringReplaceFast(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
function StringReplaceFast(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; out aCount : Integer): string;
const
MATCHESCOUNTRESIZER=100; //Arbitrary value. Memory used = MATCHESCOUNTRESIZER * sizeof(SizeInt)
var
@ -619,6 +619,7 @@ var
inc(MatchesCount);
end;
begin
aCount:=0;
if (OldPattern='') or (Length(OldPattern)>Length(S)) then begin
//This cases will never match nothing.
Result:=S;
@ -703,7 +704,8 @@ begin
end;
end;
end;
//Create room enougth for the result string
//Create room enough for the result string
aCount:=MatchesCount;
SetLength(Result,Length(S)-OldPatternSize*MatchesCount+NewPatternSize*MatchesCount);
MatchIndex:=1;
MatchTarget:=1;
@ -759,7 +761,7 @@ end;
*)
function StringReplaceBoyerMoore(const S, OldPattern, NewPattern: string;Flags: TReplaceFlags): string;
function StringReplaceBoyerMoore(const S, OldPattern, NewPattern: string;Flags: TReplaceFlags; out aCount : Integer): string;
var
Matches: SizeIntArray;
OldPatternSize: SizeInt;
@ -770,6 +772,7 @@ var
MatchInternal: SizeInt;
AdvanceIndex: SizeInt;
begin
aCount:=0;
OldPatternSize:=Length(OldPattern);
NewPatternSize:=Length(NewPattern);
if (OldPattern='') or (Length(OldPattern)>Length(S)) then begin
@ -784,6 +787,7 @@ begin
end;
MatchesCount:=Length(Matches);
aCount:=MatchesCount;
//Create room enougth for the result string
SetLength(Result,Length(S)-OldPatternSize*MatchesCount+NewPatternSize*MatchesCount);
@ -813,11 +817,21 @@ end;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Algorithm: TStringReplaceAlgorithm): string;
Var
C : Integer;
begin
Result:=StringReplace(S, OldPattern, NewPattern, Flags,C,Algorithm);
end;
Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; out aCount : Integer; Algorithm : TStringReplaceAlgorithm = sraDefault): string; overload;
begin
Case Algorithm of
sraDefault : Result:=sysutils.StringReplace(S,OldPattern,NewPattern,Flags);
sraManySmall : Result:=StringReplaceFast(S,OldPattern,NewPattern,Flags);
sraBoyerMoore : Result:=StringReplaceBoyerMoore(S,OldPattern,NewPattern,Flags);
sraDefault : Result:=sysutils.StringReplace(S,OldPattern,NewPattern,Flags,aCount);
sraManySmall : Result:=StringReplaceFast(S,OldPattern,NewPattern,Flags,aCount);
sraBoyerMoore : Result:=StringReplaceBoyerMoore(S,OldPattern,NewPattern,Flags,aCount);
end;
end;

View File

@ -118,11 +118,15 @@ Var
{ Text file support }
Procedure AssignFile(out t:Text;p:pchar);
Procedure AssignFile(out t:Text;c:char);
Procedure AssignFile(out t:Text;p:pchar; aCodePage : TSystemCodePage);
Procedure AssignFile(out t:Text;c:char; aCodePage : TSystemCodePage);
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
Procedure AssignFile(out t:Text;const Name:UnicodeString);
Procedure AssignFile(out t:Text;const Name:UnicodeString; aCodePage : TSystemCodePage);
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
Procedure AssignFile(out t:Text;const Name:RawByteString);
Procedure AssignFile(out t:Text;const Name:RawByteString; aCodePage : TSystemCodePage);
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
Procedure CloseFile(Var t:Text);
{$endif FPC_HAS_FEATURE_TEXTIO}
@ -215,12 +219,31 @@ begin
System.Assign (T,p);
end;
Procedure AssignFile(out t:Text;p:pchar; aCodePage : TSystemCodePage);
begin
System.Assign (T,p);
SetTextCodePage(T,aCodePage);
end;
Procedure AssignFile(out t:Text;c:char);
begin
System.Assign (T,c);
end;
Procedure AssignFile(out t:Text;c:char; aCodePage : TSystemCodePage);
begin
System.Assign (T,c);
SetTextCodePage(T,aCodePage);
end;
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
Procedure AssignFile(out t:Text;const Name:RawBytestring; aCodePage : TSystemCodePage);
begin
System.Assign (T,Name);
SetTextCodePage(T,aCodePage);
end;
Procedure AssignFile(out t:Text;const Name:RawBytestring);
begin
System.Assign (T,Name);
@ -228,6 +251,12 @@ end;
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
Procedure AssignFile(out t:Text;const Name:UnicodeString; aCodePage : TSystemCodePage);
begin
System.Assign (T,Name);
SetTextCodePage(T,aCodePage);
end;
Procedure AssignFile(out t:Text;const Name:UnicodeString);
begin
System.Assign (T,Name);

View File

@ -45,6 +45,7 @@ type
strict protected
FIsSingleByte: Boolean;
FMaxCharSize: Integer;
class procedure FreeEncodings;
function GetByteCount(Chars: PUnicodeChar; CharCount: Integer): Integer; overload; virtual; abstract;
function GetBytes(Chars: PUnicodeChar; CharCount: Integer; Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
function GetCharCount(Bytes: PByte; ByteCount: Integer): Integer; overload; virtual; abstract;
@ -54,7 +55,6 @@ type
function GetCodePage: Cardinal; virtual; abstract;
function GetEncodingName: UnicodeString; virtual; abstract;
public
class procedure FreeEncodings;
function Clone: TEncoding; virtual;
class function Convert(Source, Destination: TEncoding; const Bytes: TBytes): TBytes; overload;
class function Convert(Source, Destination: TEncoding; const Bytes: TBytes; StartIndex, Count: Integer): TBytes; overload;

View File

@ -1,9 +1,10 @@
var
OldPat,Srch: SRstring; // Srch and Oldp can contain uppercase versions of S,OldPattern
PatLength,NewPatLength,P,Cnt,PatCount,PrevP: Integer;
PatLength,NewPatLength,P,Cnt,PrevP: Integer;
c,d: SRPChar ;
begin
aCount:=0;
Result:='';
c:= NIL; d:=NIL;
OldPat:='';
@ -31,6 +32,7 @@ begin
repeat
P:=Pos(OldPat,Srch,P);
if P>0 then begin
inc(aCount);
move(NewPattern[1],Result[P],PatLength*SizeOf(SRChar));
if not (rfReplaceAll in Flags) then exit;
inc(P,PatLength);
@ -40,21 +42,21 @@ begin
//Different pattern length -> Result length will change
//To avoid creating a lot of temporary strings, we count how many
//replacements we're going to make.
P:=1; PatCount:=0;
P:=1;
repeat
P:=Pos(OldPat,Srch,P);
if P>0 then begin
inc(P,PatLength);
inc(PatCount);
inc(aCount);
if not (rfReplaceAll in Flags) then break;
end;
until p=0;
if PatCount=0 then begin
if aCount=0 then begin
Result:=S;
exit;
end;
NewPatLength:=Length(NewPattern);
SetLength(Result,Length(S)+PatCount*(NewPatLength-PatLength));
SetLength(Result,Length(S)+aCount*(NewPatLength-PatLength));
P:=1; PrevP:=0;
c:=SRPChar(Result); d:=SRPChar(S);
repeat

View File

@ -76,6 +76,19 @@ begin
Dest := Dest + S;
end ;
function IsLeadChar(C: AnsiChar): Boolean; inline;
begin
Result:=C in LeadBytes;
end;
function IsLeadChar(B: Byte): Boolean; inline;
begin
Result:=Char(B) in LeadBytes;
end;
Function InternalChangeCase(Const S : AnsiString; const Chars: TSysCharSet; const Adjustment: Longint): AnsiString;
var
i : Integer;
@ -2294,6 +2307,16 @@ end;
{$define SRCHAR:=Char}
Function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
Var
C : Integer;
begin
Result:=StringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Out aCount : Integer): string;
{$i syssr.inc}
{$undef INSTRINGREPLACE}

View File

@ -234,6 +234,7 @@ function TryStrToBool(const S: string; out Value: Boolean): Boolean;
function TryStrToBool(const S: string; out Value: Boolean; Const FormatSettings: TFormatSettings): Boolean;
function LastDelimiter(const Delimiters, S: string): SizeInt;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags; Out aCount : Integer): string;
function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;
Function IsDelimiter(const Delimiters, S: string; Index: SizeInt): Boolean;
@ -250,6 +251,8 @@ Function CharToByteLen(const S: string; MaxLen: SizeInt): SizeInt;
Function ByteToCharIndex(const S: string; Index: SizeInt): SizeInt;
Function StrCharLength(const Str: PChar): SizeInt;
function StrNextChar(const Str: PChar): PChar;
function IsLeadChar(C: AnsiChar): Boolean; inline; overload;
function IsLeadChar(B: Byte): Boolean; inline; overload;
const

View File

@ -540,7 +540,16 @@ end;
{$define SRPCHAR:=PUnicodeChar}
{$define SRCHAR:=UnicodeChar}
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags): UnicodeString;
Function UnicodeStringReplace(const S, OldPattern, NewPattern: Unicodestring; Flags: TReplaceFlags): Unicodestring;
Var
C : Integer;
begin
Result:=UnicodeStringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags; Out aCount : Integer): UnicodeString;
{$i syssr.inc}
{$undef INUNICODESTRINGREPLACE}

View File

@ -68,3 +68,5 @@ function WideBytesOf(const Value: UnicodeString): TBytes;
function WideStringOf(const Value: TBytes): UnicodeString;
function ByteLength(const S: UnicodeString): Integer;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags): UnicodeString;
function UnicodeStringReplace(const S, OldPattern, NewPattern: UnicodeString; Flags: TReplaceFlags; Out aCount : Integer): UnicodeString;

View File

@ -193,11 +193,21 @@ end;
{$define SRPChar:=PWideChar}
{$define SRChar:=WideChar}
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString;
Function WideStringReplace(const S, OldPattern, NewPattern: Widestring; Flags: TReplaceFlags): Widestring;
Var
C : Integer;
begin
Result:=WideStringReplace(S,OldPattern,NewPattern,Flags,C);
end;
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags; Out aCount : Integer): WideString;
{$i syssr.inc}
{$undef INWIDESTRINGREPLACE}
{$undef SRString}
{$undef SRUpperCase}
{$undef SRPChar}
{$undef SRChar}
{$undef SRChar}

View File

@ -35,6 +35,6 @@ function StrCopy(Dest, Source: PWideChar): PWideChar; overload;
function StrLCopy(Dest,Source: PWideChar; MaxLen: SizeInt): PWideChar; overload;
Function CharInSet(Ch:WideChar;Const CSet : TSysCharSet) : Boolean;
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString;
function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags; Out aCount : Integer): WideString;
function IsLeadChar(Ch: WideChar): Boolean; inline; overload;

View File

@ -917,6 +917,34 @@ begin
GetlocaleFormatSettings(GetThreadLocale, DefaultFormatSettings);
end;
Procedure InitLeadBytes;
var
I,B,C,E: Byte;
Info: TCPInfo;
begin
GetCPInfo(CP_ACP,Info);
I:=0;
With Info do
begin
B:=LeadByte[i];
E:=LeadByte[i+1];
while (I<MAX_LEADBYTES) and (B<>0) and (E<>0) do
begin
for C:=B to E do
Include(LeadBytes,AnsiChar(C));
Inc(I,2);
if (I<MAX_LEADBYTES) then
begin
B:=LeadByte[i];
E:=LeadByte[i+1];
end;
end;
end;
end;
Procedure InitInternational;
var
{ A call to GetSystemMetrics changes the value of the 8087 Control Word on
@ -1493,6 +1521,7 @@ Initialization
ExceptObjProc:=@WinExceptionObject;
ExceptClsProc:=@WinExceptionClass;
{$endif mswindows}
InitLeadBytes;
InitInternational; { Initialize internationalization settings }
LoadVersionInfo;
InitSysConfigDir;