Initial revision

This commit is contained in:
ceriel
1987-01-06 11:41:50 +00:00
parent 3788350d7c
commit a7aa5d93ff
38 changed files with 1227 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
/* bts2str() turns a row of bytes b with length n into string s
The ASCII set of characters is used.
86/03/17 EHB
*/
#define is_print(c) ((unsigned)((c) - ' ') <= '~' - ' ')
char *
bts2str(b, n, s)
char *b, *s;
register int n;
{
register char *f = b, *t = s;
while (n-- > 0) {
if (is_print(*f))
*t++ = *f++;
else {
register int n = (*f++ & 0377);
register int i;
register char *p;
*t = '\\';
p = (t += 4);
for (i = 0; i < 3; i++) {
*--p = (n & 07) + '0';
n >>= 3;
}
}
}
*t = '\000';
return s;
}

View File

@@ -0,0 +1,16 @@
/* btscat()
*/
char *
btscat(b1, n1, b2, n2)
char *b1;
int n1;
register char *b2;
register int n2;
{
register char *b = b1 + n1;
while (n2-- > 0)
*b++ = *b2++;
return b1;
}

View File

@@ -0,0 +1,16 @@
/* btscmp()
*/
int
btscmp(b1, n1, b2, n2)
register char *b1, *b2;
int n1, n2;
{
register n = (n1 <= n2) ? n1 : n2;
while (n-- > 0) {
if (*b1++ != *b2++)
return *--b1 - *--b2;
}
return n2 - n1;
}

View File

@@ -0,0 +1,14 @@
/* btscpy()
*/
char *
btscpy(b1, b2, n)
register char *b1, *b2;
register int n;
{
char *b1s = b1;
while (n-- > 0)
*b1++ = *b2++;
return b1s;
}

View File

@@ -0,0 +1,11 @@
/* btszero()
*/
char *
btszero(b, n)
register char *b;
register int n;
{
while (n-- > 0)
*b++ = '\0';
}

View File

@@ -0,0 +1,60 @@
/* Integer to String translator
-> base is a value from [-16,-2] V [2,16]
-> base < 0: see 'val' as unsigned value
-> no checks for buffer overflow and illegal parameters
(1985, EHB)
*/
#define MAXWIDTH 32
char *
long2str(val, base)
register long val;
register base;
{
static char numbuf[MAXWIDTH];
static char vec[] = "0123456789ABCDEF";
register char *p = &numbuf[MAXWIDTH];
int sign = (base > 0);
*--p = '\0'; /* null-terminate string */
if (val) {
if (base > 0) {
if (val < 0L) {
if ((val = -val) < 0L)
goto overflow;
}
else
sign = 0;
}
else
if (base < 0) { /* unsigned */
base = -base;
if (val < 0L) { /* taken from Amoeba src */
register mod, i;
overflow:
mod = 0;
for (i = 0; i < 8 * sizeof val; i++) {
mod <<= 1;
if (val < 0)
mod++;
val <<= 1;
if (mod >= base) {
mod -= base;
val++;
}
}
*--p = vec[mod];
}
}
do {
*--p = vec[(int) (val % base)];
val /= base;
} while (val != 0L);
if (sign)
*--p = '-'; /* don't forget it !! */
}
else
*--p = '0'; /* just a simple 0 */
return p;
}

View File

@@ -0,0 +1,67 @@
/* str2bts -- (1985, EHB)
*/
static
is_oct(c)
char c;
{
return (c == '0' || c == '1' || c == '2' || c == '3' ||
c == '4' || c == '5' || c == '6' || c == '7');
}
/* str2bts() strips the escaped characters of a
string and replaces them by the ascii characters they stand for.
The ascii length of the resulting string is returned, including the
terminating null-character.
*/
char *
str2bts(str, bts, pn)
register char *str;
char *bts;
int *pn;
{
register char *t = bts;
while (*str) {
if (*str == '\\') {
switch (*++str) {
case 'b':
*t++ = '\b';
str++;
break;
case 'f':
*t++ = '\f';
str++;
break;
case 'n':
*t++ = '\n';
str++;
break;
case 'r':
*t++ = '\r';
str++;
break;
case 't':
*t++ = '\t';
str++;
break;
default:
if (is_oct(*str)) {
register cnt = 0, oct = 0;
do
oct = oct * 8 + *str - '0';
while (is_oct(*++str) && ++cnt < 3);
*t++ = (char) oct;
break;
}
*t++ = *str++;
break;
}
}
else
*t++ = *str++;
}
*t = '\0'; /* don't forget this one !!! */
*pn = t - bts + 1;
return bts;
}

View File

@@ -0,0 +1,32 @@
/* str2long()
*/
value(c, b)
char c;
int b;
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return b;
}
long
str2long(str, base)
register char *str;
int base;
{
int minus = 0, d;
long l = 0;
if (*str == '-') {
minus++;
str++;
}
while ((d = value(*str++, base)) < base)
l = base * l + d;
return minus ? -l : l;
}

View File

@@ -0,0 +1,15 @@
/* append t to s
*/
char *
strcat(s, t)
register char *s, *t;
{
register char *b = s;
while (*s++)
;
s--;
while (*s++ = *t++)
;
return b;
}

View File

@@ -0,0 +1,12 @@
/* return negative, zero or positive value if
resp. s < t, s == t or s > t
*/
int
strcmp(s, t)
register char *s, *t;
{
while (*s == *t++)
if (*s++ == '\0')
return 0;
return *s - *--t;
}

View File

@@ -0,0 +1,12 @@
/* Copy t into s
*/
char *
strcpy(s, t)
register char *s, *t;
{
register char *b = s;
while (*s++ = *t++)
;
return b;
}

View File

@@ -0,0 +1,12 @@
/* strindex() -- (86/03/18 EHB)
*/
char *
strindex(s, c)
register char *s, c;
{
while (*s)
if (*s++ == c)
return --s;
return (char *)0;
}

250
modules/src/string/string.3 Normal file
View File

@@ -0,0 +1,250 @@
.TH STRING 3ACK "86/03/18"
.SH NAME
strcpy, strncpy, strcat, strncat, strcmp, strncmp,
strlen, strindex, strrindex, strzero, str2bts,
long2str, str2long,
btscpy, btscat, btscmp, btszero, bts2str \- operations on and
conversions between strings and row of bytes
.SH SYNOPSIS
.nf
.B char *strcpy(s1, s2)
.B char *s1, *s2;
.PP
.B char *strncpy(s1, s2, n)
.B char *s1, *s2;
.PP
.B char *strcat(s1, s2)
.B char *s1, *s2;
.PP
.B char *strncat(s1, s2, n)
.B char *s1, *s2;
.PP
.B int strcmp(s1, s2)
.B char *s1, *s2;
.PP
.B int strncmp(s1, s2, n)
.B char *s1, *s2;
.PP
.B int strlen(s)
.B char *s;
.PP
.B char *strindex(s, c)
.B char *s, c;
.PP
.B char *strrindex(s, c)
.B char *s, c;
.PP
.B char *strzero(s)
.B char *s;
.PP
.B char *str2bts(s, b, pn)
.B char *s, *b;
.B int *pn;
.PP
.B char *long2str(l, base)
.B long l;
.B int base;
.PP
.B long str2long(s, base)
.B char *s;
.B int base;
.PP
.B char *btscpy(b1, b2, n)
.B char *b1, *b2;
.B int n;
.PP
.B char *btscat(b1, n1, b2, n2)
.B char *b1, *b2;
.B int n1, n2;
.PP
.B int btscmp(b1, n1, b2, n2)
.B char *b1, *b2;
.B int n1, n2;
.PP
.B char *btszero(b, n)
.B char *b;
.B int n;
.PP
.B char *bts2str(b, n, s)
.B char *b, *s;
.B int n;
.fi
.SH DESCRIPTION
The
.IR str *
functions operate on null-terminated strings.
The
.IR bts *
functions operate on variable-length rows of bytes,
regardless of null bytes.
Neither of these functions check for overflow of any receiving area.
.PP
.I Strcpy
copies string
.I s2
to
.I s1,
stopping after the null character has been moved.
.I Strncpy
copies exactly
.I n
characters,
truncating or null-padding
.I s2;
the target may not be null-terminated if the length
of
.I s2
is
.I n
or more.
Both return
.IR s1 .
.PP
.I Strcat
appends a copy of string
.I s2
to the end of string
.IR s1 .
.I Strncat
copies at most
.I n
characters.
Both return a pointer to the null-terminated result
.IR s1 .
.PP
.I Strcmp
compares its arguments and returns an integer
greater than, equal to, or less than 0, if
.I s1
is lexicographically greater than, equal to, or
less than
.IR s2 ,
respectively.
.I Strncmp
makes the same comparison but checks at most
.I n
characters.
.PP
.I Strlen
returns the number of characters before the null-character.
.IR s .
.PP
.I Strindex
.RI ( strrindex )
returns a pointer to the first (last)
occurrence of character
.I c
in string
.I s,
or zero if
.I c
does not occur in
.IR s .
.PP
.I Bts2str
turns a row of
.I n
consecutive bytes, the first of which is pointed by
.IR b ,
into a null-terminated string, starting at
.IR s .
Printable characters are copied and non-printable characters are transformed
into sequences of printable characters, representing those characters.
The transformation agrees with the representation of non-printable
characters in C strings.
.br
E.g., the row of 2 bytes
.RS
\&'\e0' '\en'
.RE
is turned into the string consisting of the following characters
.RS
\&'\e\e' '0' '0' '0' '\e\e' 'n' '\e0'
.RE
The latter string could be represented in C as "\e\e000\e\en".
.PP
.I Str2bts
turns string
.I s
into a sequence of bytes pointed by
.IR b .
It has the inverse effect to
.IR bts2str .
The length of the resulting byte sequence is returned in
.RI * pn .
.br
Both the functions
.I bts2str
and
.I str2bts
return a pointer to the result.
.PP
.I Long2str
converts a long value
.I l
into a null-terminated string according to
.IR base ,
which indicates the base to use.
This base may be any of 2..16.
A negative base (in the range -16..-2) indicates that the long must be
seen as unsigned.
A pointer to the string is returned.
.I Str2long
returns the value that is represented in
.IR s ,
according to
.IR base .
.PP
.I Btscpy
copies
.I n
bytes from the string of bytes
.I b2
to
.I b1
and returns
.IR b1 .
.PP
.I Btscat
appends a copy of
.I n2
bytes from
.I b2
to the end of
.IR b1 ,
consisting of
.I n1
bytes.
.I B1
is returned.
.PP
.I Btscmp
compares row of bytes
.I b1
with length
.I n1
and
.I b2
with length
.I n2
and returns an integer greater than, equal to, or less than 0, if
.I b1
is lexicographically greater then, equal to, or less than
.IR b2 ,
respectively.
.PP
.I Btszero
places
.I n
null bytes in the string
.IR b .
.I B
is returned.
.SH FILES
~em/modules/lib/libstring.a
.SH "SEE ALSO"
string(3), bstring(3), atof(3)
.SH BUGS
No checks for overflow or illegal parameters.
.SH AUTHOR
Erik Baalbergen <erikb@vu44.UUCP>

View File

@@ -0,0 +1,12 @@
/* return length of s
*/
int
strlen(s)
char *s;
{
register char *b = s;
while (*b++)
;
return b - s - 1;
}

View File

@@ -0,0 +1,16 @@
/* append t to s, upto n characters
*/
char *
strncat(s, t, n)
register char *s, *t;
register int n;
{
register char *b = s;
while (*s++)
;
s--;
while ((n-- > 0) && (*s++ = *t++))
;
return b;
}

View File

@@ -0,0 +1,18 @@
/* return negative, zero or positive value if
resp. s < t, s == t or s > t; compare at most n characters
*/
int
strncmp(s, t, n)
register char *s, *t;
register int n;
{
while (n-- > 0) {
if (*s == *t++) {
if (*s++ == '\0')
return 0;
}
else
return *s - *--t;
}
return 0;
}

View File

@@ -0,0 +1,13 @@
/* Copy t into s, upto n characters
*/
char *
strncpy(s, t, n)
register char *s, *t;
register int n;
{
register char *b = s;
while ((n-- > 0) && (*s++ = *t++))
;
return b;
}

View File

@@ -0,0 +1,11 @@
char *
strrindex(str, chr)
register char *str, chr;
{
register char *retptr = 0;
while (*str)
if (*str++ == chr)
retptr = &str[-1];
return retptr;
}

View File

@@ -0,0 +1,9 @@
/* strzero()
*/
char *
strzero(s)
char *s;
{
*s = '\0';
}