rev 450 - trunk/src
SVN User <[email protected]> Mon, 03 May 2004 13:25:36 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: jknapka
Date: 2004-05-03 13:25:33 -0400 (Mon, 03 May 2004)
New Revision: 450
Modified:
trunk/src/builtins-string.c
Log:
Fixed length in String__mul__. Added default args where appropriate.
Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c 2004-05-03 15:41:29 UTC (rev 449)
+++ trunk/src/builtins-string.c 2004-05-03 17:25:33 UTC (rev 450)
@@ -245,7 +245,7 @@
obj = NEW_OBJ(String_OBJ);
if (tlen < IMMEDIATE_DATA_LEN) {
obj->data_type = DATA_TYPE_IMMDATA;
- obj->imm_data_len = (int) tlen;
+ obj->imm_data_len = (int) tlen-1;
for(i=0; i < times; i++)
memcpy(obj->data.str+i*len, pr_strptr(self), len);
obj->data.str[tlen] = 0;
@@ -255,7 +255,7 @@
raise_exception(ist, OBJ(OUTOFMEMORY_EXC), "memory allocation failed for string multiplication");
return NULL;
}
- obj_str->len = tlen;
+ obj_str->len = tlen-1;
for(i=0; i < times; i++)
memcpy(obj_str->str+i*len, pr_strptr(self), len);
obj_str->str[tlen] = 0;
@@ -409,11 +409,13 @@
}
// Generic case-changer function.
+#define AT_WORD_START -1
static obj_p changeCase(isp ist,obj_p self,int (*changer)(int), size_t howMany) {
size_t str_len = pr_strlen(self), ii = 0;
char* dest_ptr = 0;
obj_p obj=0;
pr_str_p obj_str=0;
+ int atStart = 1;
obj = NEW_OBJ(String_OBJ);
obj_str = obj_malloc(ist, OBJ(STRING_PROTO), obj, sizeof(pr_str_t)+str_len+1);
@@ -428,10 +430,19 @@
// Fill the copy.
char* src_ptr = pr_strptr(self);
for (ii=0; ii<str_len; ++ii) {
- if (ii<howMany) {
- dest_ptr[ii] = (char)(*changer)(src_ptr[ii]);
+ if (howMany == AT_WORD_START) {
+ if (atStart) {
+ dest_ptr[ii] = (char)(*changer)(src_ptr[ii]);
+ } else {
+ dest_ptr[ii] = src_ptr[ii];
+ }
+ if (isspace(src_ptr[ii])) atStart = 1; else atStart = 0;
} else {
- dest_ptr[ii] = src_ptr[ii];
+ if (ii<howMany) {
+ dest_ptr[ii] = (char)(*changer)(src_ptr[ii]);
+ } else {
+ dest_ptr[ii] = src_ptr[ii];
+ }
}
}
dest_ptr[str_len] = 0;
@@ -451,6 +462,10 @@
return changeCase(ist,self,&toupper,1);
}
+DEF(String, capwords, NULL) {
+ return changeCase(ist,self,&toupper,AT_WORD_START);
+}
+
// strstr()-analog that works for strings that may contain nulls.
// Totally dependent upon ASCII encoding; beware.
static char* bin_strstr(char* haystack,size_t lhaystack, char* needle,size_t lneedle) {
@@ -484,7 +499,7 @@
}
#define Int_value(objid) (objid->data.i64)
-DEF(String, find, FORM_PARAM2) {
+DEF(String, find, FPARM2( stringToFind, NULL, indexToStartLooking, NEW_INT(0) )) {
size_t str_len = pr_strlen(self);
size_t find_len = 0;
char* find_ptr = 0;
@@ -495,13 +510,11 @@
raise_exception(ist, OBJ(TYPE_EXC), "find function parameter 1 must be a string");
return NULL;
}
- if (parms[3]) {
- if (!has_proto_QUES(ist, parms[3], OBJ(INT_PROTO))) {
- raise_exception(ist, OBJ(TYPE_EXC), "find function parameter 2 must be an integer");
- return NULL;
- } else {
- start = (size_t)Int_value(parms[3]);
- }
+ if (!has_proto_QUES(ist, parms[3], OBJ(INT_PROTO))) {
+ raise_exception(ist, OBJ(TYPE_EXC), "find function parameter 2 must be an integer");
+ return NULL;
+ } else {
+ start = (size_t)Int_value(parms[3]);
}
find_ptr = pr_strptr(parms[1]);
@@ -613,15 +626,16 @@
return obj;
}
-DEF(String, expandtabs, NULL) {
- // FIX ME: should accept an int arg (default 8) telling how many
- // spaces per tab.
+DEF(String, expandtabs, FPARM1( nSpaces, NEW_INT(8) )) {
obj_p tab = new_string_obj(ist,"\t");
- obj_p spcs = new_string_obj(ist," ");
- obj_p args[] = {NULL,tab,NULL,spcs};
- obj_p result = Stringreplace( ist, self, 2, args, 0 );
+ obj_p tspace = new_string_obj(ist," ");
+ obj_p mul_args[] = {NULL,parms[1]};
+ obj_p spcs = String__mul__(ist,tspace,1,mul_args,0);
+ obj_p repl_args[] = {NULL,tab,NULL,spcs};
+ obj_p result = Stringreplace(ist,self,2,repl_args,0);
del_unlock(tab);
del_unlock(spcs);
+ del_unlock(tspace);
return result;
}
@@ -769,6 +783,7 @@
MODULE_ADD_SYM(String, upper);
MODULE_ADD_SYM(String, replace);
MODULE_ADD_SYM(String, capitalize);
+ MODULE_ADD_SYM(String, capwords);
MODULE_ADD_SYM(String, expandtabs);
MODULE_ADD_SYM(String, lstrip);
MODULE_ADD_SYM(String, rstrip);