WideString Utilities Library 1.0.5

Delphi 5, 6, 7, and Kylix Implementation

by Dieter Köhler


LICENSE

The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at "http://www.mozilla.org/MPL/"

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.

The Original Code is "WideStringUtils.pas".

The Initial Developer of the Original Code is Dieter Köhler (Heidelberg, Germany, "http://www.philo.de/"). Portions created by the Initial Developer are Copyright (C) 2003 Dieter Köhler. All Rights Reserved.

Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 2 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL, and not to allow others to use your version of this file under the terms of the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the MPL or the GPL.


Acknowledgment

Thanks to Karl Waclawek for providing the TUtilsWideStringStream class.


Introduction

The WideString Utilities Library contains helper functions for WideString. The latest version of this software is available at <http://www.philo.de/xml/>.


Using the unit

The WideString Utilities Library does not contain any components to be registered. So using it from inside your own projects is very simple: Add "WideStringUtils" to the uses clause of your unit and make sure that the path to the location of the WideStringUtils.pas file is included in Delphi's list of library paths. To include it go to the Library section of Delphi's Environment Options dialog (see the menu item: "Tools/Environment Options ...").


WideString Handling Routines and Classes

function CompareWideStr(const S1, S2: WideString): Integer;

CompareWideStr compares S1 to S2, with case-sensitivity. The return value is less than 0 if S1 is less than S2, 0 if S1 equals S2, or greater than 0 if S1 is greater than S2. The compare operation is based on the 16-bit ordinal value of each character and is not affected by the current locale. Also UTF-16 surrogates are not recognized as such, but treated according to the 16-bit ordinal value of their constituents ($D800..$DFFF), i.e. they are arranged before $E000.

function EndsWideStr(const SubString, S: WideString): Boolean;

Indicates whether one wideString is a (case-sensitive) suffix of another. The return value is True if S ends with SubString, otherwise False is returned. The comparison is based on the 16-bit ordinal value of each character and is not affected by the current locale.

function StartsWideStr(const SubString, S: WideString): Boolean;

Indicates whether one wideString is a (case-sensitive) prefix of another. The return value is True if S starts with SubString, otherwise False is returned. The comparison is based on the 16-bit ordinal value of each character and is not affected by the current locale.

function TranslateWideString(const S, Pattern_1, Pattern_2: WideString): WideString;

Returns S with occurrences of characters in Pattern_1 replaced by the character at the corresponding position in Pattern_2. For example translateWideString('bar', 'abc', 'ABC') returns the wideString 'BAr'. If there is a character in Pattern_1 with no character at a corresponding position in Pattern_2 (because Pattern_1 is longer than Pattern_2), then occurrences of that character in S are removed. For example translateWideString('--aaa--', 'abc-', "ABC") returns 'AAA'. If a character occurs more than once in Pattern_1, then the first occurrence determines the replacement character. If Pattern_2 is longer than Pattern_1, then excess characters are ignored.

function TrimSpace(const S: WideString): WideString;

Returns the specified WideString S with leading and trailing space characters (#$20) removed.

TUtilsCustomWideStr = class

TUtilsCustomWideStr can be used to improve the performance of concating WideStrings. In Delphi, concating WideStrings may lead to performance critical memory allocation ans string copying. Consider the following example:

var
  S: WideString;
  I: Intger;
begin
  for I := 0 to 99 do
    S := S + '.';
end;

Each time a new point is added to S, new memory sufficient for the expanted WideString is allocated, the bytes from the previous memory area are copied to the freshly allocated memory, the point character is added, and finally the previous memory used for S is deallocated. So the above loop triggers 100 of these allocation, copying and deallocation processes.

TUtilsCustomWideStr is a class that allows an easy to handle solution to this problem: The object uses an internal buffer that requires only occasionally to allocated new memory: It starts with a memory buffer of 64 byte; each time the memory buffer is full, it is automatically expanded by 25 %.

Public Properties
Public Methods

WideString Lists and Trees

Auxilliary Types
TUtilsWideStringItem
  TUtilsWideStringItem = record
    FString: WideString;
    FObject: TObject;
  end;

TUtilsWideStringItem is a record internally used in the TUtilsWideStringList class to store WideString-Object pairs.

TUtilsWideStringItemList = array[0..MaxListSize] of TUtilsWideStringItem;

TUtilsWideStringItemList defines a list of TUtilsWideStringItem records.

PUtilsWideStringItemList = ^TUtilsWideStringItemList;

PUtilsWideStringItemList defines a pointer to a TUtilsWideStringItemList array. It is internally used in TUtilsWideStringList.


TUtilsWideStringList = class (TPersistent)

TUtilsWideStringList is a WideString equivalent for the Delphi VCL TStringList class. In particular TUtilsWideStringList defines the following public methods and properties equivalent to TStringList (not all TStringList methods have an equivalent in TUtilsWideStringList):

    destructor Destroy; override;
    function Add(S: WideString): Integer; virtual;
    function AddObject(S: WideString; AObject: TObject): Integer; virtual;
    procedure AddWideStrings(Strings: TUtilsWideStringList); virtual;
    procedure Append(S: WideString); virtual;
    procedure Assign(Source: TPersistent); override;
    procedure BeginUpdate; virtual;
    procedure Clear; virtual;
    procedure Delete(Index: Integer); virtual;
    procedure EndUpdate; virtual;
    procedure Exchange(Index1, Index2: Integer); virtual;
    function Find(const S: WideString;
                    var Index: Integer): Boolean; virtual;
    function IndexOf(const S: WideString): Integer; virtual;
    procedure Insert(Index: Integer; const S: WideString); virtual;
    procedure InsertObject(Index: Integer; const S: WideString; AObject: TObject); virtual;
    procedure Sort; virtual;

    property Capacity: Integer read GetCapacity write SetCapacity;
    property Count: Integer read GetCount;
    property Duplicates: TDuplicates read FDuplicates write FDuplicates default dupIgnore;
    property Objects[Index: Integer]: TObject read GetObject write PutObject;
    property Sorted: Boolean read FSorted write SetSorted default False;
    property WideStrings[Index: Integer]: WideString read Get write Put; default;

    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;

The following public methods of TUtilsWideStringList have no equivalent in TStringList:


TUtilsNameValueList = class (TPersistent)

A TUtilsNameValueList is used to store name-value pairs of WideStrings.

Public Properties
Public Methods

TUtilsNameValueTree = class(TUtilsNameValueList)

A TUtilsNameValueTree object is used to store name-value pairs of WideStrings together with an optional reference to a subtree which must also be of type TUtilsNameValueTree.

The TUtilsNameValueTree object owns the TUtilsNameValueTree subtrees added to it. When the TUtilsNameValueTree is cleared or destroyed, all subtrees are recursively destroyed, too.

Properties
Public methods

WideString Streaming

TUtilsWideStringStream = class (TStream)

TdomWideStringStream is a stream interface for WideStrings. Its purpose is to avoid excessive memory re-allocations that occur with WideStrings because they are not reference counted. Note that when the dataString property is accesses, the complete WideString will be copied out.

Public Properties
Public Methods