-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paths.c
103 lines (93 loc) · 2.42 KB
/
s.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* $Id$ */
#include <string.h> /*strcpy,strlen*/
#include <assert.h>
#include "xmlc.h"
#include "m.h"
#include "s.h"
int s_cmpn(char *s1,char *s2,int n2) {
char *end=s2+n2;
for(;;++s1,++s2) {
if(s2==end) return *s1;
if(*s1=='\0') return -*s2;
if(*s1!=*s2) return *s1-*s2;
}
}
int s_tokcmpn(char *s1,char *s2,int n2) {
char *end2=s2+n2;
/* all white space characters are one byte long */
while(xmlc_white_space(*s1)) ++s1;
while(s2!=end2&&xmlc_white_space(*s2)) ++s2;
for(;;) {
if(s2==end2) {
while(xmlc_white_space(*s1)) ++s1;
return *s1;
}
if(*s1=='\0') {
while(s2!=end2&&xmlc_white_space(*s2)) ++s2;
return s2==end2?0:-*s2;
}
if(xmlc_white_space(*s1)&&xmlc_white_space(*s2)) {
do ++s1; while(xmlc_white_space(*s1));
do ++s2; while(s2!=end2&&xmlc_white_space(*s2));
} else {
if(*s1!=*s2) return *s1-*s2;
++s1; ++s2;
}
}
}
int s_hval(char *s) {
int h=0;
while(*s) h=h*31+*(s++);
return h;
}
char *s_clone(char *s) {
return strcpy((char*)m_alloc(strlen(s)+1,sizeof(char)),s);
}
char *s_abspath(char *r,char *b) {
if(*r!='/') {
char *c=b,*sep=(char*)0;
for(;;) {if(!(*c)) break; if(*c++=='/') sep=c;}
if(sep) {
char *p=r,*q;
while(*p++); q=p+(sep-b);
do *(--q)=*(--p); while(p!=r);
while(b!=sep) *r++=*b++;
}
}
return r;
}
int s_tab(char *s,char *tab[],int size) {return s_ntab(s,strlen(s),tab,size);}
int s_ntab(char *s,int len,char *tab[],int size) {
int n=0,m=size-1,i,cmp;
for(;;) {
if(n>m) return size;
i=(n+m)/2;
if((cmp=s_cmpn(tab[i],s,len))==0) return i; else {if(cmp>0) m=i-1; else n=i+1;}
}
}
void s_test() {
assert(s_cmpn("","",0)==0);
assert(s_cmpn("/xyz","/xyz",4)==0);
assert(s_cmpn("xyz","yz",2)<0);
assert(s_cmpn("xyz","xxyz",4)>0);
{ char r[256];
s_abspath(strcpy(r,"/x"),"/y");
assert(strcmp(r,"/x")==0);
s_abspath(strcpy(r,"x"),"/y");
assert(strcmp(r,"/x")==0);
s_abspath(strcpy(r,"x"),"/y/");
assert(strcmp(r,"/y/x")==0);
s_abspath(strcpy(r,"x"),"y/");
assert(strcmp(r,"y/x")==0);
s_abspath(strcpy(r,"x"),"y");
assert(strcmp(r,"x")==0);
s_abspath(strcpy(r,""),"y");
assert(strcmp(r,"")==0);
}
assert(s_tokcmpn("","",0)==0);
assert(s_tokcmpn(""," ",1)==0);
assert(s_tokcmpn("A","A",1)==0);
assert(s_tokcmpn(" A B","A B ",5)==0);
assert(s_tokcmpn("AB","A B",3)>0);
assert(s_tokcmpn("","A",1)<0);
}