1992-09-03 20:21:07 +00:00
|
|
|
/***********************************************************
|
1993-03-29 10:43:31 +00:00
|
|
|
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
|
|
|
Amsterdam, The Netherlands.
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* strop module */
|
|
|
|
|
|
|
|
#include "allobjects.h"
|
|
|
|
#include "modsupport.h"
|
|
|
|
|
1993-07-08 11:12:36 +00:00
|
|
|
#include <ctype.h>
|
1993-07-09 10:51:31 +00:00
|
|
|
/* XXX This file assumes that the <ctype.h> is*() functions
|
|
|
|
XXX are defined for all 8-bit characters! */
|
1993-07-08 11:12:36 +00:00
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
static object *
|
|
|
|
strop_split(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
1993-11-05 10:14:49 +00:00
|
|
|
int len, i, j, err;
|
1992-09-03 20:21:07 +00:00
|
|
|
char *s;
|
|
|
|
char c;
|
|
|
|
object *list, *item;
|
|
|
|
|
|
|
|
if (!getargs(args, "s#", &s, &len))
|
|
|
|
return NULL;
|
|
|
|
list = newlistobject(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < len) {
|
|
|
|
while (i < len &&
|
1993-07-08 11:12:36 +00:00
|
|
|
((c = s[i]), isspace(c))) {
|
1992-09-03 20:21:07 +00:00
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
j = i;
|
|
|
|
while (i < len &&
|
1993-07-08 11:12:36 +00:00
|
|
|
!((c = s[i]), isspace(c))) {
|
1992-09-03 20:21:07 +00:00
|
|
|
i = i+1;
|
|
|
|
}
|
|
|
|
if (j < i) {
|
|
|
|
item = newsizedstringobject(s+j, (int)(i-j));
|
1993-11-05 10:14:49 +00:00
|
|
|
if (item == NULL) {
|
|
|
|
DECREF(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
err = addlistitem(list, item);
|
|
|
|
DECREF(item);
|
|
|
|
if (err < 0) {
|
1992-09-03 20:21:07 +00:00
|
|
|
DECREF(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static object *
|
|
|
|
strop_splitfields(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
1993-11-05 10:14:49 +00:00
|
|
|
int len, n, i, j, err;
|
1992-09-03 20:21:07 +00:00
|
|
|
char *s, *sub;
|
|
|
|
object *list, *item;
|
|
|
|
|
|
|
|
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
|
|
|
return NULL;
|
|
|
|
if (n == 0) {
|
|
|
|
err_setstr(ValueError, "empty separator");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = newlistobject(0);
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = j = 0;
|
|
|
|
while (i+n <= len) {
|
|
|
|
if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) {
|
|
|
|
item = newsizedstringobject(s+j, (int)(i-j));
|
1993-11-05 10:14:49 +00:00
|
|
|
if (item == NULL)
|
|
|
|
goto fail;
|
|
|
|
err = addlistitem(list, item);
|
|
|
|
DECREF(item);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
1992-09-03 20:21:07 +00:00
|
|
|
i = j = i + n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
item = newsizedstringobject(s+j, (int)(len-j));
|
1993-11-05 10:14:49 +00:00
|
|
|
if (item == NULL)
|
|
|
|
goto fail;
|
|
|
|
err = addlistitem(list, item);
|
|
|
|
DECREF(item);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
return list;
|
1993-11-05 10:14:49 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
DECREF(list);
|
|
|
|
return NULL;
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-11-26 08:54:07 +00:00
|
|
|
static object *
|
|
|
|
strop_joinfields(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
object *seq, *item, *res;
|
|
|
|
object * (*getitem) FPROTO((object *, int));
|
|
|
|
char *sep, *p;
|
|
|
|
int seplen, seqlen, reslen, itemlen, i;
|
|
|
|
|
|
|
|
if (!getargs(args, "(Os#)", &seq, &sep, &seplen))
|
|
|
|
return NULL;
|
|
|
|
if (is_listobject(seq)) {
|
|
|
|
getitem = getlistitem;
|
|
|
|
seqlen = getlistsize(seq);
|
|
|
|
}
|
|
|
|
else if (is_tupleobject(seq)) {
|
|
|
|
getitem = gettupleitem;
|
|
|
|
seqlen = gettuplesize(seq);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_setstr(TypeError, "first argument must be list/tuple");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
reslen = 0;
|
|
|
|
for (i = 0; i < seqlen; i++) {
|
|
|
|
item = getitem(seq, i);
|
|
|
|
if (!is_stringobject(item)) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"first argument must be list/tuple of strings");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (i > 0)
|
|
|
|
reslen = reslen + seplen;
|
|
|
|
reslen = reslen + getstringsize(item);
|
|
|
|
}
|
|
|
|
if (seqlen == 1) {
|
|
|
|
/* Optimization if there's only one item */
|
|
|
|
item = getitem(seq, 0);
|
|
|
|
INCREF(item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
res = newsizedstringobject((char *)NULL, reslen);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
p = getstringvalue(res);
|
|
|
|
for (i = 0; i < seqlen; i++) {
|
|
|
|
item = getitem(seq, i);
|
|
|
|
if (i > 0) {
|
|
|
|
memcpy(p, sep, seplen);
|
|
|
|
p += seplen;
|
|
|
|
}
|
|
|
|
itemlen = getstringsize(item);
|
|
|
|
memcpy(p, getstringvalue(item), itemlen);
|
|
|
|
p += itemlen;
|
|
|
|
}
|
|
|
|
if (p != getstringvalue(res) + reslen) {
|
|
|
|
err_setstr(SystemError, "strop.joinfields: assertion failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
static object *
|
|
|
|
strop_index(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *s, *sub;
|
|
|
|
int len, n, i;
|
|
|
|
|
|
|
|
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
1993-10-26 15:23:55 +00:00
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
1992-09-03 20:21:07 +00:00
|
|
|
if (i < 0 || i+n > len) {
|
|
|
|
err_setstr(ValueError, "start offset out of range");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err_clear();
|
|
|
|
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
|
|
|
return NULL;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return newintobject((long)i);
|
|
|
|
|
|
|
|
len -= n;
|
1993-10-26 15:23:55 +00:00
|
|
|
for (; i <= len; ++i)
|
|
|
|
if (s[i] == sub[0] &&
|
|
|
|
(n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0))
|
|
|
|
return newintobject((long)i);
|
|
|
|
|
|
|
|
err_setstr(ValueError, "substring not found");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static object *
|
|
|
|
strop_rindex(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *s, *sub;
|
|
|
|
int len, n, i;
|
|
|
|
|
|
|
|
if (getargs(args, "(s#s#i)", &s, &len, &sub, &n, &i)) {
|
|
|
|
if (i < 0)
|
|
|
|
i += len;
|
|
|
|
if (i < 0 || i+n > len) {
|
|
|
|
err_setstr(ValueError, "start offset out of range");
|
|
|
|
return NULL;
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
1993-10-26 15:23:55 +00:00
|
|
|
else {
|
|
|
|
err_clear();
|
|
|
|
if (!getargs(args, "(s#s#)", &s, &len, &sub, &n))
|
|
|
|
return NULL;
|
|
|
|
i = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return newintobject((long)i);
|
|
|
|
|
|
|
|
for (i -= n; i >= 0; --i)
|
|
|
|
if (s[i] == sub[0] &&
|
|
|
|
(n == 1 || strncmp(&s[i+1], &sub[1], n-1) == 0))
|
|
|
|
return newintobject((long)i);
|
1992-09-03 20:21:07 +00:00
|
|
|
|
|
|
|
err_setstr(ValueError, "substring not found");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static object *
|
|
|
|
strop_strip(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int len, i, j;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (!getargs(args, "s#", &s, &len))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
i = 0;
|
1993-07-08 11:12:36 +00:00
|
|
|
while (i < len && ((c = s[i]), isspace(c))) {
|
1992-09-03 20:21:07 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
j = len;
|
|
|
|
do {
|
|
|
|
j--;
|
1993-07-08 11:12:36 +00:00
|
|
|
} while (j >= i && ((c = s[j]), isspace(c)));
|
1992-09-03 20:21:07 +00:00
|
|
|
j++;
|
|
|
|
|
|
|
|
if (i == 0 && j == len) {
|
|
|
|
INCREF(args);
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return newsizedstringobject(s+i, j-i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-09-11 23:55:51 +00:00
|
|
|
static object *
|
|
|
|
strop_lower(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
1993-10-22 12:04:32 +00:00
|
|
|
char *s, *s_new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int i, n;
|
|
|
|
object *new;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
if (!getargs(args, "s#", &s, &n))
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
new = newsizedstringobject(NULL, n);
|
1992-09-11 23:55:51 +00:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
s_new = getstringvalue(new);
|
1992-09-11 23:55:51 +00:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1993-10-22 12:04:32 +00:00
|
|
|
char c = *s++;
|
1992-09-11 23:55:51 +00:00
|
|
|
if (isupper(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 12:04:32 +00:00
|
|
|
*s_new = tolower(c);
|
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
DECREF(new);
|
|
|
|
INCREF(args);
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static object *
|
|
|
|
strop_upper(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
1993-10-22 12:04:32 +00:00
|
|
|
char *s, *s_new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int i, n;
|
|
|
|
object *new;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
if (!getargs(args, "s#", &s, &n))
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
new = newsizedstringobject(NULL, n);
|
1992-09-11 23:55:51 +00:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
s_new = getstringvalue(new);
|
1992-09-11 23:55:51 +00:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1993-10-22 12:04:32 +00:00
|
|
|
char c = *s++;
|
1992-09-11 23:55:51 +00:00
|
|
|
if (islower(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 12:04:32 +00:00
|
|
|
*s_new = toupper(c);
|
|
|
|
} else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
DECREF(new);
|
|
|
|
INCREF(args);
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static object *
|
|
|
|
strop_swapcase(self, args)
|
|
|
|
object *self; /* Not used */
|
|
|
|
object *args;
|
|
|
|
{
|
1993-10-22 12:04:32 +00:00
|
|
|
char *s, *s_new;
|
1992-09-11 23:55:51 +00:00
|
|
|
int i, n;
|
|
|
|
object *new;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
if (!getargs(args, "s#", &s, &n))
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
new = newsizedstringobject(NULL, n);
|
1992-09-11 23:55:51 +00:00
|
|
|
if (new == NULL)
|
|
|
|
return NULL;
|
1993-10-22 12:04:32 +00:00
|
|
|
s_new = getstringvalue(new);
|
1992-09-11 23:55:51 +00:00
|
|
|
changed = 0;
|
|
|
|
for (i = 0; i < n; i++) {
|
1993-10-22 12:04:32 +00:00
|
|
|
char c = *s++;
|
1992-09-11 23:55:51 +00:00
|
|
|
if (islower(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 12:04:32 +00:00
|
|
|
*s_new = toupper(c);
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
else if (isupper(c)) {
|
|
|
|
changed = 1;
|
1993-10-22 12:04:32 +00:00
|
|
|
*s_new = tolower(c);
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
1993-10-22 12:04:32 +00:00
|
|
|
else
|
|
|
|
*s_new = c;
|
|
|
|
s_new++;
|
1992-09-11 23:55:51 +00:00
|
|
|
}
|
|
|
|
if (!changed) {
|
|
|
|
DECREF(new);
|
|
|
|
INCREF(args);
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1992-09-03 20:21:07 +00:00
|
|
|
/* List of functions defined in the module */
|
|
|
|
|
|
|
|
static struct methodlist strop_methods[] = {
|
|
|
|
{"index", strop_index},
|
1992-11-26 08:54:07 +00:00
|
|
|
{"joinfields", strop_joinfields},
|
1992-09-11 23:55:51 +00:00
|
|
|
{"lower", strop_lower},
|
1993-10-26 15:23:55 +00:00
|
|
|
{"rindex", strop_rindex},
|
1992-09-03 20:21:07 +00:00
|
|
|
{"split", strop_split},
|
|
|
|
{"splitfields", strop_splitfields},
|
|
|
|
{"strip", strop_strip},
|
1992-09-11 23:55:51 +00:00
|
|
|
{"swapcase", strop_swapcase},
|
|
|
|
{"upper", strop_upper},
|
1992-09-03 20:21:07 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
initstrop()
|
|
|
|
{
|
1993-07-08 11:12:36 +00:00
|
|
|
object *m, *d, *s;
|
|
|
|
char buf[256];
|
|
|
|
int c, n;
|
|
|
|
m = initmodule("strop", strop_methods);
|
|
|
|
d = getmoduledict(m);
|
1993-07-09 10:51:31 +00:00
|
|
|
|
|
|
|
/* Create 'whitespace' object */
|
1993-07-08 11:12:36 +00:00
|
|
|
n = 0;
|
|
|
|
for (c = 1; c < 256; c++) {
|
|
|
|
if (isspace(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1993-09-14 08:37:39 +00:00
|
|
|
s = newsizedstringobject(buf, n);
|
1993-07-09 10:51:31 +00:00
|
|
|
if (s) {
|
|
|
|
dictinsert(d, "whitespace", s);
|
|
|
|
DECREF(s);
|
|
|
|
}
|
|
|
|
/* Create 'lowercase' object */
|
|
|
|
n = 0;
|
|
|
|
for (c = 1; c < 256; c++) {
|
|
|
|
if (islower(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
|
|
|
s = newsizedstringobject(buf, n);
|
|
|
|
if (s) {
|
|
|
|
dictinsert(d, "lowercase", s);
|
|
|
|
DECREF(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create 'uppercase' object */
|
|
|
|
n = 0;
|
|
|
|
for (c = 1; c < 256; c++) {
|
|
|
|
if (isupper(c))
|
|
|
|
buf[n++] = c;
|
|
|
|
}
|
1993-07-08 11:12:36 +00:00
|
|
|
s = newsizedstringobject(buf, n);
|
1993-07-09 10:51:31 +00:00
|
|
|
if (s) {
|
|
|
|
dictinsert(d, "uppercase", s);
|
|
|
|
DECREF(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err_occurred())
|
|
|
|
fatal("can't initialize module strop");
|
1992-09-03 20:21:07 +00:00
|
|
|
}
|