String enhancements. Slice deletion
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4629 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
ae2e14741f
commit
732612cac3
5 changed files with 89 additions and 0 deletions
|
|
@ -459,6 +459,21 @@ DohInsertitem(DOH *obj, int index, const DOH *value) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* DohDelslice()
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
int
|
||||
DohDelslice(DOH *obj, int sindex, int eindex) {
|
||||
DohBase *b = (DohBase *) obj;
|
||||
DohObjInfo *objinfo = b->type;
|
||||
if (objinfo->doh_list && objinfo->doh_list->doh_delslice) {
|
||||
return (objinfo->doh_list->doh_delslice)(b,sindex,eindex);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* DohFirstitem()
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@
|
|||
#define DohSetitem DOH_NAMESPACE(Setitem)
|
||||
#define DohDelitem DOH_NAMESPACE(Delitem)
|
||||
#define DohInsertitem DOH_NAMESPACE(Insertitem)
|
||||
#define DohDelslice DOH_NAMESPACE(Delslice)
|
||||
#define DohFirstitem DOH_NAMESPACE(Firstitem)
|
||||
#define DohNextitem DOH_NAMESPACE(Nextitem)
|
||||
#define DohWrite DOH_NAMESPACE(Write)
|
||||
|
|
@ -87,6 +88,7 @@
|
|||
#define DohIsString DOH_NAMESPACE(IsString)
|
||||
#define DohIsFile DOH_NAMESPACE(IsFile)
|
||||
#define DohNewString DOH_NAMESPACE(NewString)
|
||||
#define DohNewStringWithSize DOH_NAMESPACE(NewStringWithSize)
|
||||
#define DohNewStringf DOH_NAMESPACE(NewStringf)
|
||||
#define DohStrcmp DOH_NAMESPACE(Strcmp)
|
||||
#define DohStrncmp DOH_NAMESPACE(Strncmp)
|
||||
|
|
@ -190,6 +192,7 @@ extern DOH *DohGetitem(DOH *obj, int index);
|
|||
extern int DohSetitem(DOH *obj, int index, const DOHObj_or_char *value);
|
||||
extern int DohDelitem(DOH *obj, int index);
|
||||
extern int DohInsertitem(DOH *obj, int index, const DOHObj_or_char *value);
|
||||
extern int DohDelslice(DOH *obj, int sindex, int eindex);
|
||||
extern DOH *DohFirstitem(DOH *obj);
|
||||
extern DOH *DohNextitem(DOH *obj);
|
||||
|
||||
|
|
@ -243,6 +246,7 @@ extern int DohGetmark(DOH *obj);
|
|||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
extern DOHString *DohNewString(const DOH *c);
|
||||
extern DOHString *DohNewStringWithSize(const DOH *c, int len);
|
||||
extern DOHString *DohNewStringf(const DOH *fmt, ...);
|
||||
|
||||
extern int DohStrcmp(const DOHString_or_char *s1, const DOHString_or_char *s2);
|
||||
|
|
@ -310,6 +314,7 @@ extern DOH *DohNone;
|
|||
#define Setitem DohSetitem
|
||||
#define Delitem DohDelitem
|
||||
#define Insert DohInsertitem
|
||||
#define Delslice DohDelslice
|
||||
#define Append(s,x) DohInsertitem(s,DOH_END,x)
|
||||
#define Push(s,x) DohInsertitem(s,DOH_BEGIN,x)
|
||||
#define Len DohLen
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ typedef struct {
|
|||
int (*doh_setitem)(DOH *obj, int index, DOH *value); /* Set item */
|
||||
int (*doh_delitem)(DOH *obj, int index); /* Delete item */
|
||||
int (*doh_insitem)(DOH *obj, int index, DOH *value); /* Insert item */
|
||||
int (*doh_delslice)(DOH *obj, int sindex, int eindex); /* Delete slice */
|
||||
DOH *(*doh_firstitem)(DOH *obj); /* Iterators */
|
||||
DOH *(*doh_nextitem)(DOH *obj);
|
||||
} DohListMethods;
|
||||
|
|
|
|||
|
|
@ -299,6 +299,7 @@ static DohListMethods ListListMethods = {
|
|||
List_set,
|
||||
List_remove,
|
||||
List_insert,
|
||||
0, /* delslice */
|
||||
List_first,
|
||||
List_next,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -271,6 +271,36 @@ String_delitem(DOH *so, int pos)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* int String_delslice() - Delete a range
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
String_delslice(DOH *so, int sindex, int eindex) {
|
||||
String *s = (String *) ObjData(so);
|
||||
int size;
|
||||
s->hashkey = -1;
|
||||
if (eindex == DOH_END) eindex = s->len;
|
||||
if (sindex == DOH_BEGIN) sindex = 0;
|
||||
if (s->len == 0) return 0;
|
||||
|
||||
size = eindex - sindex;
|
||||
if (s->sp > eindex) {
|
||||
/* Adjust the file pointer and line count */
|
||||
char *c = s->str + sindex;
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
if (*c == '\n') s->line--;
|
||||
}
|
||||
s->sp -= size;
|
||||
assert(s->sp >= 0);
|
||||
}
|
||||
memmove(s->str+sindex,s->str+eindex, ((s->len-size) - sindex));
|
||||
s->len -= size;
|
||||
s->str[s->len] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* DOH *String_str() - Returns a string (used by printing commands)
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
|
@ -843,6 +873,7 @@ static DohListMethods StringListMethods = {
|
|||
0, /* doh_setitem */
|
||||
String_delitem, /* doh_delitem */
|
||||
String_insert, /* doh_insitem */
|
||||
String_delslice, /* doh_delslice */
|
||||
0, /* doh_first */
|
||||
0, /* doh_next */
|
||||
};
|
||||
|
|
@ -924,6 +955,42 @@ DohNewString(const DOH *so)
|
|||
return DohObjMalloc(&DohStringType,str);
|
||||
}
|
||||
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* NewStringWithSize(const char *c, int len) - Create a new string
|
||||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
DOHString *
|
||||
DohNewStringWithSize(const DOH *so, int len)
|
||||
{
|
||||
int l = 0, max;
|
||||
String *str;
|
||||
char *s;
|
||||
if (DohCheck(so)) s = Char(so);
|
||||
else s = (char *) so;
|
||||
str = (String *) DohMalloc(sizeof(String));
|
||||
str->hashkey = -1;
|
||||
str->sp = 0;
|
||||
str->line = 1;
|
||||
str->file = 0;
|
||||
max = INIT_MAXSIZE;
|
||||
if (s) {
|
||||
l = (int) len;
|
||||
if ((l+1) > max) max = l+1;
|
||||
}
|
||||
str->str = (char *) DohMalloc(max);
|
||||
str->maxsize = max;
|
||||
if (s) {
|
||||
strncpy(str->str,s,len);
|
||||
str->len = l;
|
||||
str->sp = l;
|
||||
} else {
|
||||
str->str[0] = 0;
|
||||
str->len = 0;
|
||||
}
|
||||
return DohObjMalloc(&DohStringType,str);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* NewStringf(DOH *fmt, ...)
|
||||
*
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue