|
|
ru.algorithms- RU.ALGORITHMS ---------------------------------------------------------------- From : Anton Morozov 2:5051/36.6 08 Oct 2002 06:12:45 To : All Subject : маска --------------------------------------------------------------------------------
Есть маска, есть строка. В маске любые символы и "*" с "?" Hада проверить,
попадает ли строка в маску. Вроде написал, тока как-то криво... Мож получше
можно? Мучался пол дня...
Turbo Pascal.
{ Выдает подстроку из строки S с символа X1 до X2 }
function str_get(s:String; x1,x2:byte):string;
var w:string; i:byte;
begin
str_get:='';
if x1<1 then x1:=1; if x1>ord(s[0]) then exit;
if x2<1 then exit; if x2>ord(s[0]) then x2:=ord(s[0]);
w:=''; for i:=x1 to x2 do w:=w+s[i];
str_get:=w;
end;
{ Ищет в строке S с символа номер X символ, входящий в множество C. }
function str_find(s:string; c:charset; x:byte):byte;
begin
while (x<=ord(s[0])) and not (s[x] in c) do inc(x);
if x>ord(s[0]) then str_find:=0 else str_find:=x;
end;
{ Поиск подстроки W в строке S с позиции X }
function str_search(s,w:string; x:byte):byte;
var sn:byte absolute s; wn:byte absolute w; k:byte;
begin
k:=0;
while x<=sn-wn+1 do begin
if w[1]=s[x] then begin
k:=0;
while k<wn do begin
if w[k+1]<>s[x+k] then break; inc(k); end;
if k=wn then begin str_search:=x; exit; end;
end;
inc(x);
end;
str_search:=0;
end;
{ S - строка, M - маска, результат - подходит или нет...}
function str_mask(s,m:string):boolean;
var x,xx,k:byte; w:string;
begin
x:=1; xx:=1; str_mask:=false;
while x<=ord(s[0]) do begin
if xx>ord(m[0]) then exit;
if (m[xx]='*') then begin
if xx=ord(m[0]) then break;
inc(xx); k:=str_find(m,['*','?'],xx);
if k=0 then k:=ord(m[0]) else dec(k);
w:=str_get(m,xx,k); x:=str_search(s,w,x);
if x=0 then exit; continue; end else
if (m[xx]='?') then begin
if xx=ord(m[0]) then if x=ord(s[0]) then break else exit;
inc(x); inc(xx); continue; end else
if (m[xx]=s[x]) then begin
inc(x); inc(xx); end
else exit;
end;
str_mask:=true;
end;
Тьма с нами, All...
[Cannabis Club] [Turbo Pascal] [Куннилингус] [Assembler] [Альфа Йод Первитин]
--- [Stopped]
* Origin: [Uptime] 00:17:45:59 [пpикинь, это под виндой-то!] (2:5051/36.6)
Вернуться к списку тем, сортированных по: возрастание даты уменьшение даты тема автор
Архивное /ru.algorithms/27903da29522.html, оценка из 5, голосов 10
|