Skip to content

Instantly share code, notes, and snippets.

@K4zoku
Last active January 25, 2019 13:13
Show Gist options
  • Save K4zoku/82a5a9a43d7ab194bde235b5a984bb51 to your computer and use it in GitHub Desktop.
Save K4zoku/82a5a9a43d7ab194bde235b5a984bb51 to your computer and use it in GitHub Desktop.
(*
*
* @Author <a href="https://github.com/takahatashun"> TakahataShun </a>
* @Version <b> 1.0 </b>
*
*)
unit StringUtils;
interface
uses sysUtils, crt;
(****************************************************************************)
const
NULL = #000;
SOL = #001;
STX = #002;
ETX = #003;
EOT = #004;
ENQ = #005;
ACK = #006;
BEL = #007;
BS = #008;
HT = #009;
LF = #010;
VT = #011;
FF = #012;
CR = #013;
SO = #014;
SI = #015;
DLE = #016;
DC1 = #017;
DC2 = #018;
DC3 = #019;
DC4 = #020;
NAK = #021;
SYN = #022;
ETB = #023;
CAN = #024;
EM = #025;
SUB = #026;
ESC = #027;
FS = #028;
GS = #029;
RS = #030;
US = #031;
SPACE = #032;
DEL = #127;
EMPTY = '';
(****************************************************************************)
type
Strings = array of String;
Chars = array of Char;
(****************************************************************************)
function equals (const txt1, txt2: String) : Boolean;
function isEmpty (const txt: String) : Boolean;
function countChar (const txt: String; ch: Char) : Integer;
function stringValueOf (const int: LongInt) : String;
function split (txt: String; Seperate: Char) : Strings;
function toCharArray (const txt: String) : Chars;
function deleteUnusedSpaces (var txt: String) : String;
function upperCaseFirstLetter (var txt: String) : String;
procedure print (msg: String);
procedure print (txtColor: Byte; msg: String);
procedure print (txtColor, backColor: Byte; msg: String);
procedure println (msg: String);
procedure println (txtColor: Byte; msg: String);
procedure println (txtColor, backColor: Byte; msg: String);
(****************************************************************************)
implementation
function equals (const txt1, txt2: String): Boolean;
begin
result:= (txt1 = txt2);
end;
function isEmpty (const txt: String): Boolean;
begin
result:= equals(txt, EMPTY) or equals(txt, NULL);
end;
function countChar (const txt: String; ch: Char): Integer;
var
i, len: Byte;
begin
result:= 0;
len:= length(txt);
for i:= 1 to Len do
if equals(txt[i], ch) then inc(result);
end;
function stringValueOf (const int: LongInt): String;
var
s: String;
begin
str(int, s);
result:= s;
end;
function split (txt: String; Seperate: Char): Strings;
var
count, i, len, posDel: Byte;
temp: String;
stringList: Strings;
begin
len:= length(txt);
count:= countChar(txt, Seperate);
posDel:= Pos(Seperate, txt);
setlength(stringList, Count+1);
if Count = 0 then stringList[1]:= txt
else
for i:=1 to Count do
begin
temp:= Copy(txt, 1, PosDel - 1);
deleteUnusedSpaces(Temp);
stringList[i]:= Temp;
txt:= Copy(txt, PosDel + 1, Len);
len:= length(txt);
posDel:= Pos(Seperate, txt);
stringList[i + 1]:= txt;
end;
result:= stringList;
end;
function toCharArray (const txt: String): Chars;
var
i, len: Integer;
begin
len:= length(txt);
setlength(result, len);
for i:=1 to len do
result[i]:= txt[i];
end;
function deleteUnusedSpaces (var txt: String): String;
var
i, len, curLen: Integer;
begin
txt:= trim(txt);
len:= length(txt);
i:= 1;
while i <= len do
begin
if (equals(txt[i], SPACE) and equals(txt[i+1], SPACE)) then
delete(txt, i, 1)
else
if (equals(txt[i], SPACE) and equals(txt[i-1], SPACE)) then
delete(txt, i, 1);
curLen:= length(txt);
if curLen <> len then
begin
len:= curLen;
i:= 1;
end
else
inc(i);
end;
result:= txt;
end;
function upperCaseFirstLetter (var txt: String): String;
var
i, len: Byte;
begin
len:= length(txt);
i:= 1;
while i <= len do
begin
if not equals(txt[i], SPACE) then
if equals(txt[i-1], SPACE) or (i = 1) then
txt[i]:= upCase(txt[i])
else
if not equals(txt[i-1], SPACE) then
txt[i]:= lowerCase(txt[i]);
inc(i);
end;
result:= txt;
end;
procedure print(msg: String);
begin
write(msg);
end;
procedure print(txtColor: Byte; msg: String);
begin
textColor(txtColor);
write(msg);
end;
procedure print(txtColor, backColor: Byte; msg: String);
begin
textColor(txtColor);
textBackground(backColor);
write(msg);
end;
procedure println(msg: String);
begin
writeln(msg);
end;
procedure println(txtColor: Byte; msg: String);
begin
textColor(txtColor);
writeln(msg);
end;
procedure println(txtColor, backColor: Byte; msg: String);
begin
textColor(txtColor);
textBackground(backColor);
writeln(msg);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment