From 0bca3d7f2bd790a39f86ebe80f340a48af5f132b Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Fri, 8 Apr 2016 03:10:13 +0300 Subject: [PATCH] sfs: using locale-insensitive comparisons --- src/sfs/sfs.pas | 50 ++++++++++++++++++++++++++++++---------- src/sfs/sfsMemFS.pas | 4 ++-- src/sfs/sfsPlainFS.pas | 30 ++++++++++++------------ src/sfs/sfsZipFS.pas | 12 +++++----- src/shared/WADEDITOR.pas | 8 +++---- 5 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/sfs/sfs.pas b/src/sfs/sfs.pas index 9615d4b..f79413a 100644 --- a/src/sfs/sfs.pas +++ b/src/sfs/sfs.pas @@ -195,10 +195,7 @@ function SFSFileList (const dataFileName: TSFSString): TSFSFileList; function SFSReplacePathDelims (const s: TSFSString; newDelim: TSFSChar): TSFSString; // èãíîðèðóåò ðåãèñòð ñèìâîëîâ -// <0: s0 < s1 -// =0: s0 = s1 -// >0: s0 > s1 -function SFSStrComp (const s0, s1: TSFSString): Integer; +function SFSStrEqu (const s0, s1: TSFSString): Boolean; // ðàçîáðàòü òîëñòîå èìÿ ôàéëà, âåðíóòü âèðòóàëüíîå èìÿ ïîñëåäíåãî ñïèñêà // èëè ïóñòóþ ñòîðîêó, åñëè ñïèñêîâ íå áûëî. @@ -519,7 +516,7 @@ begin vi := TVolumeInfo(volumes[f]); if not onlyPerm or vi.fPermanent then begin - if SFSStrComp(vi.fPackName, dataFileName) = 0 then + if SFSStrEqu(vi.fPackName, dataFileName) then begin result := f; exit; @@ -546,12 +543,41 @@ begin end; end; -// <0: s0 < s1 -// =0: s0 = s1 -// >0: s0 > s1 -function SFSStrComp (const s0, s1: TSFSString): Integer; +function le2upper (ch: Char): Char; begin - result := AnsiCompareText(s0, s1); + if ch < #128 then + begin + if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32); + end + else + begin + if (ch >= #224) and (ch <= #255) then + begin + Dec(ch, 32); + end + else + begin + case ch of + #184, #186, #191: Dec(ch, 16); + #162, #179: Dec(ch); + end; + end; + end; + result := ch; +end; + +function SFSStrEqu (const s0, s1: TSFSString): Boolean; +var + i: Integer; +begin + //result := (AnsiCompareText(s0, s1) == 0); + result := false; + if length(s0) <> length(s1) then exit; + for i := 1 to length(s0) do + begin + if le2upper(s0[i]) <> le2upper(s1[i]) then exit; + end; + result := true; end; function SFSReplacePathDelims (const s: TSFSString; newDelim: TSFSChar): TSFSString; @@ -755,8 +781,8 @@ begin Dec(result); if fFiles[result] <> nil then begin - if (SFSStrComp(fPath, TSFSFileInfo(fFiles[result]).fPath) = 0) and - (SFSStrComp(fName, TSFSFileInfo(fFiles[result]).fName) = 0) then exit; + if SFSStrEqu(fPath, TSFSFileInfo(fFiles[result]).fPath) and + SFSStrEqu(fName, TSFSFileInfo(fFiles[result]).fName) then exit; end; end; result := -1; diff --git a/src/sfs/sfsMemFS.pas b/src/sfs/sfsMemFS.pas index 2256e7f..03c8be2 100644 --- a/src/sfs/sfsMemFS.pas +++ b/src/sfs/sfsMemFS.pas @@ -205,8 +205,8 @@ end; function TSFSMemoryVolumeFactory.IsMyVolumePrefix (const prefix: TSFSString): Boolean; begin result := - (SFSStrComp(prefix, 'mem') = 0) or - (SFSStrComp(prefix, 'slh!') = 0); + SFSStrEqu(prefix, 'mem') or + SFSStrEqu(prefix, 'slh!'); end; procedure TSFSMemoryVolumeFactory.Recycle (vol: TSFSVolume); diff --git a/src/sfs/sfsPlainFS.pas b/src/sfs/sfsPlainFS.pas index bda1268..c96dbf9 100644 --- a/src/sfs/sfsPlainFS.pas +++ b/src/sfs/sfsPlainFS.pas @@ -386,7 +386,7 @@ begin if c <> f then begin // link can't be linked to itself - if SFSStrComp(TSFSExtFileInfo(fFiles[c]).fName, fi.fLink) = 0 then break; + if SFSStrEqu(TSFSExtFileInfo(fFiles[c]).fName, fi.fLink) then break; end; Inc(c); end; @@ -667,20 +667,20 @@ end; function TSFSPlainVolumeFactory.IsMyVolumePrefix (const prefix: TSFSString): Boolean; begin result := - (SFSStrComp(prefix, 'pak') = 0) or - //(SFSStrComp(prefix, 'wad') = 0) or // sorry - (SFSStrComp(prefix, 'wad2') = 0) or - (SFSStrComp(prefix, 'grp') = 0) or - (SFSStrComp(prefix, 'spe') = 0) or - (SFSStrComp(prefix, 'spec') = 0) or - (SFSStrComp(prefix, 'quake') = 0) or - (SFSStrComp(prefix, 'doom') = 0) or - (SFSStrComp(prefix, 'duke3d') = 0) or - (SFSStrComp(prefix, 'abuse') = 0) or - (SFSStrComp(prefix, 'allegro') = 0) or - (SFSStrComp(prefix, 'dune2') = 0) or - (SFSStrComp(prefix, 'max') = 0) or - (SFSStrComp(prefix, 'sin') = 0); + SFSStrEqu(prefix, 'pak') or + //SFSStrEqu(prefix, 'wad') or // sorry + SFSStrEqu(prefix, 'wad2') or + SFSStrEqu(prefix, 'grp') or + SFSStrEqu(prefix, 'spe') or + SFSStrEqu(prefix, 'spec') or + SFSStrEqu(prefix, 'quake') or + SFSStrEqu(prefix, 'doom') or + SFSStrEqu(prefix, 'duke3d') or + SFSStrEqu(prefix, 'abuse') or + SFSStrEqu(prefix, 'allegro') or + SFSStrEqu(prefix, 'dune2') or + SFSStrEqu(prefix, 'max') or + SFSStrEqu(prefix, 'sin'); end; procedure TSFSPlainVolumeFactory.Recycle (vol: TSFSVolume); diff --git a/src/sfs/sfsZipFS.pas b/src/sfs/sfsZipFS.pas index b3a8324..5b6dafd 100644 --- a/src/sfs/sfsZipFS.pas +++ b/src/sfs/sfsZipFS.pas @@ -481,12 +481,12 @@ end; function TSFSZipVolumeFactory.IsMyVolumePrefix (const prefix: TSFSString): Boolean; begin result := - (SFSStrComp(prefix, 'zip') = 0) or - (SFSStrComp(prefix, 'jar') = 0) or - (SFSStrComp(prefix, 'fout2') = 0) or - (SFSStrComp(prefix, 'vtdb') = 0) or - (SFSStrComp(prefix, 'wad') = 0) or - (SFSStrComp(prefix, 'dfwad') = 0); + SFSStrEqu(prefix, 'zip') or + SFSStrEqu(prefix, 'jar') or + SFSStrEqu(prefix, 'fout2') or + SFSStrEqu(prefix, 'vtdb') or + SFSStrEqu(prefix, 'wad') or + SFSStrEqu(prefix, 'dfwad'); end; procedure TSFSZipVolumeFactory.Recycle (vol: TSFSVolume); diff --git a/src/shared/WADEDITOR.pas b/src/shared/WADEDITOR.pas index 8da2b38..79f14b6 100644 --- a/src/shared/WADEDITOR.pas +++ b/src/shared/WADEDITOR.pas @@ -152,8 +152,8 @@ begin begin fi := fIter.Files[f]; if fi = nil then continue; - //e_WriteLog(Format('DFWAD: searching for [%s : %s] in [%s]; current is [%s : %s] (%d, %d)', [Section, Resource, fFileName, fi.path, fi.name, SFSStrComp(fi.path, Section), SFSStrComp(fi.name, Resource)]), MSG_NOTIFY); - if (SFSStrComp(fi.path, Section) = 0) and (SFSStrComp(fi.name, Resource) = 0) then + //e_WriteLog(Format('DFWAD: searching for [%s : %s] in [%s]; current is [%s : %s] (%d, %d)', [Section, Resource, fFileName, fi.path, fi.name, SFSStrEqu(fi.path, Section), SFSStrEqu(fi.name, Resource)]), MSG_NOTIFY); + if SFSStrEqu(fi.path, Section) and SFSStrEqu(fi.name, Resource) then begin // i found her! //fn := fFileName+'::'+fi.path+fi.name; @@ -195,7 +195,7 @@ begin begin fi := fIter.Files[f]; if fi = nil then continue; - if SFSStrComp(fi.path, Section) = 0 then + if SFSStrEqu(fi.path, Section) then begin SetLength(result, Length(result)+1); result[high(result)] := fi.name; @@ -215,7 +215,7 @@ begin if not FileExists(rfn) then begin //if length(rfn) >= 4 then e_WriteLog(Format('XXXX TWADEditor_1.ReadFile: [%s] [%s]', [Copy(rfn, length(rfn)-3, 4), Copy(rfn, 1, length(rfn)-4)]), MSG_NOTIFY); - if (length(rfn) >= 4) and (SFSStrComp(Copy(rfn, length(rfn)-3, 4), '.wad') = 0) then + if (length(rfn) >= 4) and SFSStrEqu(Copy(rfn, length(rfn)-3, 4), '.wad') then begin rfn := Copy(rfn, 1, length(rfn)-4); if FileExists(rfn+'.pk3') then rfn := rfn+'.pk3' -- 2.29.2