From 2cd4641da9ff97c4c5bf923a21c3c3cccd764a51 Mon Sep 17 00:00:00 2001 From: leitner Date: Mon, 28 Oct 2002 20:15:03 +0000 Subject: [PATCH] add stralloc_diff and stralloc_diffs --- CHANGES | 1 + stralloc.h | 19 ++++++++++++++++++- stralloc/stralloc_diff.3 | 13 +++++++++++++ stralloc/stralloc_diff.c | 14 ++++++++++++++ stralloc/stralloc_diffs.3 | 14 ++++++++++++++ stralloc/stralloc_diffs.c | 14 ++++++++++++++ stralloc/stralloc_starts.3 | 2 +- t.c | 18 ++++++++++++++++++ 8 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 stralloc/stralloc_diff.3 create mode 100644 stralloc/stralloc_diff.c create mode 100644 stralloc/stralloc_diffs.3 create mode 100644 stralloc/stralloc_diffs.c diff --git a/CHANGES b/CHANGES index fb001b1..3dcd6db 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,7 @@ add buffer_putsa, buffer_get_token_sa and buffer_getline_sa extended uudecode test. See comment at top for details. fix #include in ndelay*.3 (Hynek Schlawack) + add stralloc_diff and stralloc_diffs (my invention) 0.14: avoid bus errors in byte_copy diff --git a/stralloc.h b/stralloc.h index 49bf2ea..d540749 100644 --- a/stralloc.h +++ b/stralloc.h @@ -1,6 +1,13 @@ #ifndef STRALLOC_H #define STRALLOC_H +#ifdef __dietlibc__ +#include +#endif +#ifndef __pure__ +#define __pure__ +#endif + /* stralloc is the internal data structure all functions are working on. * s is the string. * len is the used length of the string. @@ -66,7 +73,17 @@ extern int stralloc_append(stralloc* sa,const char* in); /* beware: this takes a /* stralloc_starts returns 1 if the \0-terminated string in "in", without * the terminating \0, is a prefix of the string stored in sa. Otherwise * it returns 0. sa must already be allocated. */ -extern int stralloc_starts(stralloc* sa,const char* in); +extern int stralloc_starts(stralloc* sa,const char* in) __pure__; + +/* stralloc_diff returns negative, 0, or positive, depending on whether + * a is lexicographically smaller than, equal to, or greater than the + * string b. */ +extern int stralloc_diff(const stralloc* a,const stralloc* b) __pure__; + +/* stralloc_diffs returns negative, 0, or positive, depending on whether + * a is lexicographically smaller than, equal to, or greater than the + * string b[0], b[1], ..., b[n]=='\0'. */ +extern int stralloc_diffs(const stralloc* a,const char* b) __pure__; /* stralloc_0 appends \0 */ #define stralloc_0(sa) stralloc_append(sa,"") diff --git a/stralloc/stralloc_diff.3 b/stralloc/stralloc_diff.3 new file mode 100644 index 0000000..b0bb154 --- /dev/null +++ b/stralloc/stralloc_diff.3 @@ -0,0 +1,13 @@ +.TH stralloc_diffs 3 +.SH NAME +stralloc_diffs \- compare two strallocs +.SH SYNTAX +.B #include + +int \fBstralloc_diff\fP(const stralloc* \fIa\fR,const stralloc* \fIb\fR); +.SH DESCRIPTION +stralloc_diff returns negative, 0, or positive, depending on whether +\fIa\fR is lexicographically smaller than, equal to, or greater than the +string \fIb\fR. +.SH "SEE ALSO" +stralloc_diffs(3), stralloc_starts(3) diff --git a/stralloc/stralloc_diff.c b/stralloc/stralloc_diff.c new file mode 100644 index 0000000..9e00378 --- /dev/null +++ b/stralloc/stralloc_diff.c @@ -0,0 +1,14 @@ +#include "stralloc.h" +#include "byte.h" +#include "str.h" + +extern int stralloc_diff(const stralloc* a,const stralloc* b) { + register unsigned int i; + register int j; + for (i=0;;++i) { + if (i==a->len) return i==b->len?0:-1; if (i==b->len) return 1; + if ((j=(a->s[i]-b->s[i]))) return j; + } + return j; +} + diff --git a/stralloc/stralloc_diffs.3 b/stralloc/stralloc_diffs.3 new file mode 100644 index 0000000..baf03db --- /dev/null +++ b/stralloc/stralloc_diffs.3 @@ -0,0 +1,14 @@ +.TH stralloc_diffs 3 +.SH NAME +stralloc_diffs \- check if string is prefix of stralloc +.SH SYNTAX +.B #include + +int \fBstralloc_diffs\fP(stralloc* \fIa\fR,const char* \fIb\fR); +.SH DESCRIPTION +stralloc_diffs returns negative, 0, or positive, depending on whether +the \\0-terminated string in \fIa\fR, without +the terminating \\0, is lexicographically smaller than, equal to, or +greater than the string stored in \fIa\fR. +.SH "SEE ALSO" +stralloc_diff(3), stralloc_starts(3), str_diff(3) diff --git a/stralloc/stralloc_diffs.c b/stralloc/stralloc_diffs.c new file mode 100644 index 0000000..2b51675 --- /dev/null +++ b/stralloc/stralloc_diffs.c @@ -0,0 +1,14 @@ +#include "stralloc.h" +#include "byte.h" +#include "str.h" + +extern int stralloc_diffs(const stralloc* a,const char* b) { + register unsigned int i; + register int j; + for (i=0;;++i) { + if (i==a->len) return (!b[i])?0:-1; if (!b[i]) return 1; + if ((j=(a->s[i]-b[i]))) return j; + } + return j; +} + diff --git a/stralloc/stralloc_starts.3 b/stralloc/stralloc_starts.3 index c05e643..f673ad8 100644 --- a/stralloc/stralloc_starts.3 +++ b/stralloc/stralloc_starts.3 @@ -6,7 +6,7 @@ stralloc_starts \- check if string is prefix of stralloc int \fBstralloc_starts\fP(stralloc* \fIsa\fR,const char* \fIin\fR); .SH DESCRIPTION -stralloc_starts returns 1 if the \\0-terminated string in \fIbuf\fR, without +stralloc_starts returns 1 if the \\0-terminated string in \fIin\fR, without the terminating \\0, is a prefix of the string stored in \fIsa\fR. Otherwise it returns 0. \fIsa\fR must already be allocated. .SH "SEE ALSO" diff --git a/t.c b/t.c index d7106a7..f4b2775 100644 --- a/t.c +++ b/t.c @@ -22,8 +22,26 @@ __asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx") int main(int argc,char* argv[]) { +#if 0 + static stralloc s,t; + stralloc_copys(&s,"fnord"); + stralloc_copys(&t,"abc"); printf("%d\n",stralloc_diff(&s,&t)); + stralloc_copys(&t,"fnor"); printf("%d\n",stralloc_diff(&s,&t)); + stralloc_copys(&t,"fnord"); printf("%d\n",stralloc_diff(&s,&t)); + stralloc_copys(&t,"fnordh"); printf("%d\n",stralloc_diff(&s,&t)); + stralloc_copys(&t,"hausen"); printf("%d\n",stralloc_diff(&s,&t)); +#endif + static stralloc s; + stralloc_copys(&s,"fnord"); + printf("%d\n",stralloc_diffs(&s,"abc")); + printf("%d\n",stralloc_diffs(&s,"fnor")); + printf("%d\n",stralloc_diffs(&s,"fnord")); + printf("%d\n",stralloc_diffs(&s,"fnordh")); + printf("%d\n",stralloc_diffs(&s,"hausen")); +#if 0 printf("%d\n",case_starts("fnordhausen","FnOrD")); printf("%d\n",case_starts("fnordhausen","blah")); +#endif #if 0 char buf[]="FnOrD"; case_lowers(buf);