Page 1 of 2 1 2 LastLast
Results 1 to 15 of 25

Thread: NLDRcsStrings

  1. #1

    NLDRcsStrings

    ------------
    De sources van NLDDanyUtils zijn te vinden in:
    ftp://ftp.nldelphi.com/public/OpenSo.../NLDDanyUtils/
    ------------

    Hieronder de diverse string utilities in deze unit:
    Code:
    interface
    
    uses Classes,
      Windows;
    
    type
      TrimTStringsMode = (trsTrim, trsLeft, trsRight, trsTop, trsBottom,
        trsEmptyLines);
      TrimTStringsModes = set of TrimTStringsMode;
      // Any combination of the following values can be used as "Modes":
      // trsTrim:  executes a "Trim" on every string in the stringlist
      // trsLeft:  executes a "TrimLeft" on every string in the stringlist
      // trsRight: executes a "TrimRight" on every string in the stringlist
      // trsTop:   deletes all empty strings (after trimming) at the top of the TStringList
      // trsBottom: deletes all empty strings (after trimming) at the bottom of the TStringList
      // trsEmptyLines: deletes all empty strings (after trimming)
    
      CenterMode = (cmLeft, cmMid, cmRight);
    
      TRcsStrings = class(TStringList)
      private
      public
        constructor Create;
        destructor Destroy; override;
        procedure LoadFromFileInclude(Filename: string);
        procedure AddFromFileInclude(FileName: string);
        function ToCSV: string;
        procedure FromCSV(const CSV: string);
        function ToString(const Separator: Char = ','): string;
        procedure FromString(const S: string; const Separator: Char = ',');
        procedure TrimStrings(const Modes: TrimTStringsModes = [trsTrim]);
      end;
    
    function CompareKeys(KeysA: array of string; KeysB: array of string): integer; overload;
    // First KeysA[0] is compared with KeysB[0]; if they are the same then
    // KeysA[1] is compared with KeysB[1].. until a difference is encountered
    // or the last string in each "keys" is processed.
    // The function returns
    //   zero if both key groups are entirely the same (A=B),
    //   +1 if A > B, or -1 if B > A
    
    function CompareKeys(KeysA: TStrings; KeysB: TStrings): integer; overload;
    // See above function
    
    function TStringsToCSV(T: TStrings): string;
    // Returns a "comma separated value" string with stringlist "T" as contents
    
    procedure CSVToTStrings(const CSV: string; T: TStrings);
    // Converts a "comma separated value" string into in stringlist "T"
    
    function ShortenedString(S: string; const L: Integer): string;
    // Returns S if length(S) <= L; else S is returned shortened with dots (3) in the middle; the
    // total length of the returned string is then L
    
    function QuotedString(S: string; const Conditionally: Boolean = false): string;
    // Returns S with leading and trailing double quotes added. If Conditionally is true,
    // the double quotes are added only if the string contains spaces or is empty.
    
    function UnquotedString(S: string): string;
    // Returns S without its leading and trailing double quotes.
    
    function MakeCommandLine(const Cmds: array of string): string;
    // Makes a command line out of the command and parameters in "Cmds":
    // surrounds command and parameters which contain spaces with double quotes
    
    function ExcludeTrailingCrLf(S: string): string;
    // Removes the trailing CR or LF or CRLF sequence at the end of a string
    // after "Right" trimming first.
    
    procedure StringToTStrings(const S: string; T: TStrings; const Separator: Char = ',');
    // StringToTStrings converts a separator delimited string into a Stringlist
    
    function TStringsToString(T: TStrings; const Separator: Char = ','): string;
    // TStringsToString converts a Stringlist into a separator delimited string
    
    procedure TrimTStrings(T: TStrings; const Modes: TrimTStringsModes = [trsTrim]);
    // see type "TrimTStringsModes" above
    
    function BeginString(Source: string; const P: Integer): string; overload;
    // BeginString returns the part (of Source) left of position P
    
    function BeginString(Source, Tag: string): string; overload;
    // BeginString returns the part (of Source) before the Tag
    // If the Tag is not found, an empty string is returned
    // Only the first occurance of Tag in Source is taken into account
    
    function EndString(Source: string; const P: Integer): string; overload;
    // EndString returns the part (of Source) from position P onwards (the character on
    // position P included
    
    function EndString(Source, Tag: string): string; overload;
    // EndString returns the part (of Source) after the Tag
    // If the Tag is not found, an empty string is returned
    // Only the first occurance of Tag in Source is taken into account
    
    procedure SplitString(Source: string; const P: Integer; var Left, Right:
      string); overload;
    // Returns in Left and Right respectively the "BeginString" and the "EndString"
    // See the two functions above
    
    procedure SplitString(Source: string; const Tag: string; var Left, Right:
      string);
    overload;
    // Returns in Left and Right respectively the "BeginString" and the "EndString"
    // See the two functions above
    
    function CopyString(Source: string; const Start, Stop: Integer): string;
    overload;
    // Returns the part of Source from position Start to Position Stop, the characters on
    // both positions included
    
    function CopyString(Source: string; const Tag1, Tag2: string): string;
    overload;
    // Returns the part of the Source between Tag1 and Tag2
    
    function DeleteString(Source: string; const Start, Stop: Integer): string;
    overload;
    // Returns the Source with the part deleted from the Start to and including the Stop position
    
    function DeleteString(Source, Tag1, Tag2: string; const All: Boolean =
      false): string; overload;
    // Returns Source with the text between the 2 tags deleted (also the tags
    // themselves are deleted). If "All" is true, then all occurances are deleted, if false only
    // the first one is deleted. If Tag1 or Tag2 is not present, the Source is returned unchanged
    
    procedure DeleteStringProc(var Source: string; const Start, Stop: Integer);
    overload;
    // Returns the Source with the part deleted from the Start to and including the Stop position
    
    procedure DeleteStringProc(var Source: string; const Tag1, Tag2: string; const All:
      Boolean =
      false); overload;
    // Returns Source with the text between the 2 tags deleted (also the tags
    // themselves are deleted). If "All" is true, then all occurances are deleted, if false only
    // the first one is deleted. If Tag1 or Tag2 is not present, the Source is returned unchanged
    
    type
      TPosNSkipTable = array[Char] of Integer;
    
    function PosN(const SubStr, Source: string; const P: Integer = 1): Integer; overload;
    // Same as the "Pos" function, but the search starts at position P (1.. length(Source))
    
    function PosN(const SubStr, Source: string; const Stable: TPosNSkipTable; const P:
      Integer = 1): Integer; overload;
    // Same as the "PosN" function but with an external SkipTable passed as parameter
    // Execute "PosNInit" (see below) to create the skiptable before calling this function!
    
    procedure PosNInit(var Table: TPosNSkipTable; const SubStr: string);
    // Initialises the PosNSkipTable before usage of the above version of PosN
    
    procedure FillString(const SubString: string; var S: string; Position: Word);
    // Fillstring returns "S" with "SubString" at place "Position" in it.
    // The original characters as Position "Position" in "S" are overwritten.
    // The Length of the string "S" is adapted if necessary.
    
    function CenterString(S: string; const Mode: CenterMode; const Len: Word): string;
    // Centerstring returns "S" placed in a string of Length "Len" at the
    // place (cmLeft, cmMiddle or cmRight) defined by "CenterMode"
    
    function SpaceString(const Len: Word): string;
    // SpaceString returns a string of Length "Len" containing all spaces
    
    function DayMonthYearString: string;
    // DayMonthYearString returns a string containing the date of today in the
    // format: dd-mm-yy (e.g. 20-12-49 for Dec 20th 1949)
    
    function YYMMDDString: string;
    // YYMMDDString returns a string containing the date of today in the
    // format: yymmdd (e.g. 491220 for Dec 20th 1949)
    
    function YYYYMMDDString: string;
    // YYYYMMDDString returns a string containing the date of today in the
    // format: yyyymmdd (e.g. 19491220 for Dec 20th 1949)
    
    function TimeString: string;                      {hr:min:sec}
    // TimeString rerurns the current time in the
    // format: hh:mm:ss (e.g. 23:50:55 for 23 hrs, 50 minutes and 55 secs)
    
    function IntString(const I: Longint; const Len: Byte): string;
    // IntString returns a string of Length "Len",
    // representing "I" right aligned
    
    function StringIsIn(S1, S2: string; const Separator: Char = ','): Boolean;
    // StringIsIn returns true if S1 is in the "Separator separated" strings in S2
    
    function BinString(const B: Byte): string;
    // BinString returns a string with the binary representation of "B".
    // The two nibbles are separated with an underscore
    
    function LeftPad(const S: string; Len: integer; Ch: Char = ' '): string;
    // Generates a string consisting of "S" left padded with characters "Ch"
    // till length "Len" is reached.
    
    function RightPad(const S: string; Len: integer; Ch: Char = ' '): string;
    // Generates a string consisting of "S" right padded with characters "Ch"
    // till length "Len" is reached.
    
    function FloatToString(const V: Real): string;
    // FloatToString converts the floating-point value given by V to its string representation.
    // The conversion uses general number format with 15 significant digits.
    // The scientific notation ("E") is never used.
    
    function IsNumber(S: string): boolean;
    // IsNumeric checks if S is a valid number (allows floating point non scientific notation)
    
    function DateToWeekNo(Dat: string): string;
    // Returns the weeknumber of date Dat. The format of the result is "yymm.d".
    // "d" is the number of the day, 1 = monday
    // only valid from year 2000 till 2099
    
    function WeekNoToDate(WeekNo: string): string;
    // Returns the date corresponding to the weeknumber "WeekNo"
    // only valid from year 2000 till 2099
    
    Last edited by Dany; 17-Feb-08 at 13:39.

  2. #2
    Hoi Delphiërs,

    Er is een nieuwe versie:

    De procedure "TrimTStrings" heeft een extra parameter gekregen: "Modes".

    De mogelijke waarden voor "Modes" zijn:
    trsTrim, trsLeft, trsRight, trsTop, trsBottom en trsEmptyLines. Iedere kombinatie van waarden is mogelijk. De default waarde van "Modes" is "trsTrim".

    De names van de modes spreken voor zich. Voor meer detail, zie in de unit de commentaar en de code zelf.

    Veel plezier.
    Last edited by Dany; 10-Jun-04 at 17:10.
    Vriendelijke groeten,
    Dany

  3. #3
    Hoi,

    Er zijn weer een paar procedures en functies toegevoegd:
    "BeginString", "EndString", "SplitString" en "CopyString".

    Voor meer details, zie de commentaar en de code van de unit zelf.
    Vriendelijke groeten,
    Dany

  4. #4
    Hoi,

    De unit is uitgebreid met de volgende routines:
    "BeginString", "EndString", "SplitString", "CopyString", "DeleteString" en "PosN".
    Zie de eerste post in deze thread voor de interface van de unit.

    Veel plezier.
    Vriendelijke groeten,
    Dany

  5. #5
    De functie "PosN" is nu meer ge-optimaliseerd.
    Vriendelijke groeten,
    Dany

  6. #6
    Er is een fout verholpen in "PosN": als de parameter "P" (de startpositie) negatief was dan ging het fout. Bedankt voor de opmerking D7EE! (zie thread http://www.nldelphi.com/forum/showth...482#post123482)
    Vriendelijke groeten,
    Dany

  7. #7
    Er is weer een nieuwe versie.

    De wijzigingen:

    1. "PosN" (bepalen van de posities van een zoekstring in een string vanaf een bepaalde positie) kan nu (optioneel) gebruik maken van "FastStrings". Enkel de FastStrings "{$Define ..}" in het begin van de file aan- of afzetten.
    Reden: "FastPos" uit "FastStrings" is toch niet te kloppen qua snelheid.

    2. "DeleteString" en "DeleteStringProc" (de versies die een stuk van een string weghalen tussen 2 tags) zijn nu sneller (en moeilijker begrijpbaar, alhoewel..).
    Met dank aan de tip van PsychoMark (zie post http://www.nldelphi.com/forum/showth...9472#post29472).
    Last edited by Dany; 26-Jul-04 at 16:51.
    Vriendelijke groeten,
    Dany

  8. #8
    Hoi, weer een nieuwe versie:

    De PosN functie is nu veel sneller (een simpel Boyer-Moore algorthme wordt nu gebruikt in plaats van "Copy" en "Pos").
    Verder heeft de unit nu een "NoDebug" "define" die alle runtijd kostende compilerdirectives afzet.
    Vriendelijke groeten,
    Dany

  9. #9
    Hoi Iedereen,

    PosN:

    StephanEggermon: Wat 'm nog traag maakt is het iedere keer initialiseren van de skip-tabel. Als je herhaaldelijk naar dezelfde tekst in dezelfde source zoekt, kun je de skip-tabel beter als parameter meegeven.
    Het telkens weer aanmaken van een skiptabel kost inderdaad veel tijd indien telkens dezelfde string moet gezocht worden. Daarom heb in de "PosN" functie opgesplitst in 2:
    - PosNInit: het aanmaken van de skiptabel, en
    - PosN: het zoeken zelf. Deze routine krijgt nu een skiptabel als parameter mee.


    Het gebruik van deze versie van PosN kun je zien in "DeleteString" functie in deze unit.

    De originele versie van PosN (die telkens de skiptabel aanmaakt) heb ik "overloaded" behouden.
    Last edited by Dany; 11-Jul-04 at 13:25.
    Vriendelijke groeten,
    Dany

  10. #10
    Hoi,

    Vandaag een kleine functie toegevoegd:

    Code:
    function ExcludeTrailingCrLf(const S: string): string;
    De naam zelf zegt wat die doet...

    Veel plezier.
    Last edited by Dany; 29-Jul-04 at 16:24.
    Vriendelijke groeten,
    Dany

  11. #11
    Hoi,

    Weer een functie toegevoegd:
    Code:
    function MakeCommandLine(Cmds: array of string): string;
    // Makes a command line out of the command and parameters in "Cmds":
    // surrounds command and parameters which contain spaces with double quotes
    
    Deze functie kan gebruikt worden om het probleem gemeld in thread http://www.nldelphi.com/forum/showth...922#post130922 op te lossen.
    Last edited by Dany; 04-Aug-04 at 22:11.

  12. #12
    Hoi,

    2 functies toegevoegd:
    Code:
    function QuotedString(const S: string; Conditionally: boolean = false): string;
    // Returns S with leading and trailing double quotes added. If Conditionally is true,
    // the double quotes are added only if the string contains spaces or is empty.
    
    function UnquotedString(const S: string): string;
    // Returns S without its leading and trailing double quotes.
    
    Veel plezier...
    Last edited by Dany; 11-Oct-04 at 17:41.

  13. #13

    Smile

    Hoi, de volgende aanpassingen zijn gebeurd in NLDRcsStrings:

    * De "StringIsIn" functie geeft nu "True" terug als beide strings leeg zijn.

    * Toegevoegd: de function "ShortenedString" (levert een string met leading dots op als de initieele string te lang is om ergens te kunnen getoond te worden)

    * "FillString" en "CenterString" zijn nu veel sneller.

    * Een foutje uit "PosN" gehaald: de startindex mag nu buiten de target string liggen.

    * "StringToTStrings" is nu wat sneller, en een trailing separator levert een extra string op (zoals het hoort).

    * "TrimTStrings" met mode "trsTop" is nu heel wat sneller (50x).

    Last edited by Dany; 13-Feb-05 at 11:11.

  14. #14
    Deze keer 2 routines toegevoegd:

    "TStringsToCSV" en "CSVToTStrings", beide voor het afhandelen van "CSV"strings ("comma separated values": een aantal losse strings samengebracht in 1 string, gescheiden door comma's).

    De interface:
    Code:
    function TStringsToCSV(T: TStrings): string;
    // Returns a "comma separated value" string with stringlist "T" as content
    
    procedure CSVToTStrings(const CSV: string; T: TStrings);
    // Converts a "comma separated value" string into in stringlist "T"
    Voor een gerelateerd artikel, zie: http://www.creativyst.com/Doc/Articl...htm#FileFormat

    p.s. de standaard TStringList property "CommaText" doet bijna precies hetzelfde als beide bovenstaande routines, alleen neemt CommaText een spatie in een niet gequote string ook als separator (terwijl - per definitie - de separator de comma is). Spijtig. Andersom doet CommaText het wel volgens de regels: strings met een comma erin worden tussen quotes gezet.

  15. #15
    Weer een toevoeging aan NLDRcsStrings:
    Code:
    function CompareKeys(KeysA: array of String; KeysB:array of String): integer;
    // First KeysA[0] is compared with KeysB[0]; if they are the same then
    // KeysA[1] is compared with KeysB[1].. until a difference is encountered
    // or the last string in each "keys" is processed.
    // The function returns
    //   zero if both key groups are entirely the same (A=B),
    //   +1 if A > B, or -1 if B > A


    Veel plezier...

Page 1 of 2 1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •