home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
ArsClip
/
source.zip
/
UnitToken.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2003-06-05
|
3KB
|
114 lines
unit UnitToken;
{
Purpose:
String utils related to tokenization.
Just look at all the bad naming conventions - shamefull.
Updates:
Included apostropes in wordChars for words like
"Can't" and "Don't".
}
interface
function TokenString(var s: string; delim: string) : string;
function clipOneChar(s : string) : string;
function clipManyChars(s : string; clip : string) : string; overload;
function clipManyChars(s : string; count : cardinal) : string; overload;
function CharCount(s : string; c : char) : cardinal;
function CutWord(var s: string; WordChars: string) : string;
function CapitalizeWords(s : string) : string;
implementation
uses strutils, sysutils;
function CharCount(s : string; c : char) : cardinal;
var i : integer;
begin
result := 0;
for i := 1 to length(s) do begin
if s[i] = c then inc(Result);
end;
end;
function CapitalizeWords(s : string) : string;
var word : string;
letter : string;
const wordChars : string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ0123456789''';
begin
result := '';
while (s <> '') do begin
word := CutWord(s, wordchars);
if (word <> '') then begin
letter := UpperCase(word[1]);
word[1] := letter[1];
end;
result := result + word;
end;
end;
function CutWord(var s: string; WordChars: string) : string;
var i : longint;
begin
i := 1;
// find a "word"
while (i < length(s))
and (pos(s[i],WordChars) > 0) do begin
inc(i);
end;
// find all chars leading up to the next word
// stop before getting to the next word
while ((i+1) < length(s))
and (pos(s[i+1],WordChars) = 0) do begin
inc(i);
end;
// cut off the word and trailing delimiters
result := LeftStr(s, i);
s := rightstr(s, length(s) - i);
end;
function TokenString(var s: string; delim: string) : string;
var delimPos: integer;
begin
s := trimleft(s);
delimPos := pos(delim, s);
If (delimPos > 0) Then begin
//'-- cut off first token and ditch the delimiter
result := Leftstr(s, (delimPos - 1));
s := rightstr(s, length(s) - (length(delim) + length(result)));
end Else begin
//'-- return last token
result := s;
s := '';
End;
end;
function clipOneChar(s : string) : string;
begin
result := rightstr(s, length(s) - 1);
end;
function clipManyChars(s : string; clip : string) : string;
begin
result := rightstr(s, length(s) - length(clip));
end;
function clipManyChars(s : string; count : cardinal) : string;
begin
result := rightstr(s, cardinal(length(s)) - count);
end;
end.