Skip to content

Commit

Permalink
kernel: some tweaks and tests for stringobj.c
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed May 31, 2018
1 parent 8b7643e commit 329bc6c
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 116 deletions.
197 changes: 83 additions & 114 deletions src/stringobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ void LoadChar( Obj c )

/****************************************************************************
**
*F FuncEmptyString( <self>, <len> ) . . . . . . . empty string with space
*
* Returns an empty string, but with space for len characters preallocated.
*
*F FuncEmptyString( <self>, <len> ) . . . . . . . . empty string with space
**
** Returns an empty string, but with space for len characters preallocated.
**
*/
Obj FuncEmptyString( Obj self, Obj len )
{
Obj new;
while ( ! IS_INTOBJ(len) ) {
while ( ! IS_NONNEG_INTOBJ(len) ) {
len = ErrorReturnObj(
"<len> must be an integer (not a %s)",
"<len> must be an non-negative integer (not a %s)",
(Int)TNAM_OBJ(len), 0L,
"you can replace <len> via 'return <len>;'" );
}
Expand All @@ -220,11 +220,11 @@ Obj FuncEmptyString( Obj self, Obj len )

/****************************************************************************
**
*F FuncShrinkAllocationString( <self>, <str> ) . . give back unneeded memory
*
* Shrinks the bag of <str> to minimal possible size (possibly converts to
* compact representation).
*
*F FuncShrinkAllocationString( <self>, <str> ) . . give back unneeded memory
**
** Shrinks the bag of <str> to minimal possible size (possibly converts to
** compact representation).
**
*/
Obj FuncShrinkAllocationString( Obj self, Obj str )
{
Expand Down Expand Up @@ -406,19 +406,22 @@ Obj FuncSTRING_SINTLIST (
* integers ? */

/* general code */
if (! IS_RANGE(val) ) {
if (! IS_PLIST(val)) {
while (!IS_RANGE(val) && !IS_PLIST(val)) {
again:
val = ErrorReturnObj(
"<val> must be a plain list or range, not a %s)",
"<val> must be a plain list of small integers or a range, not a %s",
(Int)TNAM_OBJ(val), 0L,
"you can replace <val> via 'return <val>;'" );
}
}
if (! IS_RANGE(val) ) {
l=LEN_PLIST(val);
n=NEW_STRING(l);
p=CHARS_STRING(n);
for (i=1;i<=l;i++) {
*p++=CHAR_SINT(INT_INTOBJ(ELM_PLIST(val,i)));
Obj x = ELM_PLIST(val,i);
if (!IS_INTOBJ(x))
goto again;
*p++=CHAR_SINT(INT_INTOBJ(x));
}
}
else {
Expand All @@ -434,7 +437,6 @@ Obj FuncSTRING_SINTLIST (

}

CHANGED_BAG(n);
return n;
}

Expand Down Expand Up @@ -464,12 +466,10 @@ Obj FuncREVNEG_STRING (
q=CHARS_STRING(n);
j=l-1;
for (i=1;i<=l;i++) {
/* *q++=CHAR_SINT(-SINT_CHAR(p[j])); */
*q++=-p[j];
j--;
}

CHANGED_BAG(n);
return n;
}

Expand Down Expand Up @@ -672,7 +672,7 @@ void CleanStringCopy (
void PrintString (
Obj list )
{
char PrStrBuf[10007]; /* 7 for a \c\123 at the end */
char PrStrBuf[10007]; /* 7 for a \c\123 at the end */
UInt scanout, n;
UInt1 c;
UInt len = GET_LEN_STRING(list);
Expand All @@ -682,10 +682,10 @@ void PrintString (
{
scanout = 0;
do
{
c = CHARS_STRING(list)[off++];
switch (c)
{
{
c = CHARS_STRING(list)[off++];
switch (c)
{
case '\\':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = '\\';
Expand All @@ -694,35 +694,35 @@ void PrintString (
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = '\"';
break;
case '\n':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'n';
break;
case '\t':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 't';
break;
case '\r':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'r';
break;
case '\b':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'b';
break;
case '\01':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = '>';
break;
case '\02':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = '<';
break;
case '\03':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'c';
break;
default:
case '\n':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'n';
break;
case '\t':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 't';
break;
case '\r':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'r';
break;
case '\b':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'b';
break;
case '\01':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = '>';
break;
case '\02':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = '<';
break;
case '\03':
PrStrBuf[scanout++] = '\\';
PrStrBuf[scanout++] = 'c';
break;
default:
if (c < 32 || c>126) {
PrStrBuf[scanout++] = '\\';
n = c / 64;
Expand All @@ -735,8 +735,8 @@ void PrintString (
}
else
PrStrBuf[scanout++] = c;
}
}
}
}
while (off < len && scanout < 10000);
PrStrBuf[scanout++] = '\0';
Pr( "%s", (Int)PrStrBuf, 0L );
Expand Down Expand Up @@ -1084,8 +1084,6 @@ void AssString (

/* now perform the assignment and return the assigned value */
SET_ELM_STRING( list, pos, val );
/* CHARS_STRING(list)[pos-1] = CHAR_VALUE(val); */
CHANGED_BAG( list );
}
}

Expand Down Expand Up @@ -1377,7 +1375,6 @@ void ConvString (
ResizeBag( string, SIZEBAG_STRINGLEN(lenString) );
/* copy data area from tmp */
memcpy(ADDR_OBJ(string), CONST_ADDR_OBJ(tmp), SIZE_OBJ(tmp));
CHANGED_BAG(string);
}


Expand Down Expand Up @@ -1431,31 +1428,13 @@ Obj MakeString2(const Char *cstr1, const Char *cstr2)
return result;
}

Obj MakeString3(const Char *cstr1, const Char *cstr2, const Char *cstr3)
{
Obj result;
size_t len1 = strlen(cstr1), len2 = strlen(cstr2), len3 = strlen(cstr3);
result = NEW_STRING(len1 + len2 + len3);
memcpy(CSTR_STRING(result), cstr1, len1);
memcpy(CSTR_STRING(result)+len1, cstr2, len2);
memcpy(CSTR_STRING(result)+len1+len2, cstr3, len3);
return result;
}

Obj MakeImmString2(const Char *cstr1, const Char *cstr2)
{
Obj result = MakeString2(cstr1, cstr2);
MakeImmutableString(result);
return result;
}

Obj MakeImmString3(const Char *cstr1, const Char *cstr2, const Char *cstr3)
{
Obj result = MakeString3(cstr1, cstr2, cstr3);
MakeImmutableString(result);
return result;
}

Obj ConvImmString(Obj str)
{
Obj result;
Expand Down Expand Up @@ -1511,12 +1490,11 @@ Obj FuncCONV_STRING (
Obj string )
{
/* check whether <string> is a string */
if ( ! IS_STRING( string ) ) {
while ( ! IS_STRING( string ) ) {
string = ErrorReturnObj(
"ConvString: <string> must be a string (not a %s)",
(Int)TNAM_OBJ(string), 0L,
"you can replace <string> via 'return <string>;'" );
return FuncCONV_STRING( self, string );
}

/* convert to the string representation */
Expand Down Expand Up @@ -1549,12 +1527,11 @@ Obj FuncCOPY_TO_STRING_REP (
Obj obj )
{
/* check whether <obj> is a string */
if (!IS_STRING(obj)) {
while (!IS_STRING(obj)) {
obj = ErrorReturnObj(
"ConvString: <string> must be a string (not a %s)",
"CopyToStringRep: <string> must be a string (not a %s)",
(Int)TNAM_OBJ(obj), 0L,
"you can replace <string> via 'return <string>;'" );
return FuncCOPY_TO_STRING_REP( self, obj );
}
return CopyToStringRep(obj);
}
Expand Down Expand Up @@ -1645,12 +1622,11 @@ Obj FuncNormalizeWhitespace (
Int i, j, len, white;

/* check whether <string> is a string */
if ( ! IsStringConv( string ) ) {
while ( ! IsStringConv( string ) ) {
string = ErrorReturnObj(
"NormalizeWhitespace: <string> must be a string (not a %s)",
(Int)TNAM_OBJ(string), 0L,
"you can replace <string> via 'return <string>;'" );
return FuncNormalizeWhitespace( self, string );
}

len = GET_LEN_STRING(string);
Expand Down Expand Up @@ -1701,21 +1677,19 @@ Obj FuncREMOVE_CHARACTERS (
UInt1 REMCHARLIST[256] = {0};

/* check whether <string> is a string */
if ( ! IsStringConv( string ) ) {
while ( ! IsStringConv( string ) ) {
string = ErrorReturnObj(
"RemoveCharacters: first argument <string> must be a string (not a %s)",
(Int)TNAM_OBJ(string), 0L,
"you can replace <string> via 'return <string>;'" );
return FuncREMOVE_CHARACTERS( self, string, rem );
}

/* check whether <rem> is a string */
if ( ! IsStringConv( rem ) ) {
while ( ! IsStringConv( rem ) ) {
rem = ErrorReturnObj(
"RemoveCharacters: second argument <rem> must be a string (not a %s)",
(Int)TNAM_OBJ(rem), 0L,
"you can replace <rem> via 'return <rem>;'" );
return FuncREMOVE_CHARACTERS( self, string, rem );
}

/* set REMCHARLIST by setting positions of characters in rem to 1 */
Expand Down Expand Up @@ -1757,32 +1731,30 @@ Obj FuncTranslateString (
Int j, len;

/* check whether <string> is a string */
if ( ! IsStringConv( string ) ) {
while ( ! IsStringConv( string ) ) {
string = ErrorReturnObj(
"RemoveCharacters: first argument <string> must be a string (not a %s)",
"TranslateString: first argument <string> must be a string (not a %s)",
(Int)TNAM_OBJ(string), 0L,
"you can replace <string> via 'return <string>;'" );
return FuncTranslateString( self, string, trans );
}

/* check whether <trans> is a string */
if ( ! IsStringConv( trans ) ) {
trans = ErrorReturnObj(
"RemoveCharacters: second argument <trans> must be a string (not a %s)",
(Int)TNAM_OBJ(trans), 0L,
"you can replace <trans> via 'return <trans>;'" );
return FuncTranslateString( self, string, trans );
}

/* check if string has length at least 256 */
if ( GET_LEN_STRING( trans ) < 256 ) {
trans = ErrorReturnObj(
"RemoveCharacters: second argument <trans> must have length >= 256",
0L, 0L,
"you can replace <trans> via 'return <trans>;'" );
return FuncTranslateString( self, string, trans );
// check whether <trans> is a string of length at least 256
while ( ! IsStringConv( trans ) || GET_LEN_STRING( trans ) < 256 ) {
if ( ! IsStringConv( trans ) ) {
trans = ErrorReturnObj(
"TranslateString: second argument <trans> must be a string (not a %s)",
(Int)TNAM_OBJ(trans), 0L,
"you can replace <trans> via 'return <trans>;'" );
}

if ( GET_LEN_STRING( trans ) < 256 ) {
trans = ErrorReturnObj(
"TranslateString: second argument <trans> must have length >= 256",
0L, 0L,
"you can replace <trans> via 'return <trans>;'" );
}
}

/* now change string in place */
len = GET_LEN_STRING(string);
s = CHARS_STRING(string);
Expand Down Expand Up @@ -1816,30 +1788,27 @@ Obj FuncSplitStringInternal (
UInt1 SPLITSTRINGWSPACE[256] = { 0 };

/* check whether <string> is a string */
if ( ! IsStringConv( string ) ) {
while ( ! IsStringConv( string ) ) {
string = ErrorReturnObj(
"SplitString: first argument <string> must be a string (not a %s)",
(Int)TNAM_OBJ(string), 0L,
"you can replace <string> via 'return <string>;'" );
return FuncSplitStringInternal( self, string, seps, wspace );
}

/* check whether <seps> is a string */
if ( ! IsStringConv( seps ) ) {
while ( ! IsStringConv( seps ) ) {
seps = ErrorReturnObj(
"SplitString: second argument <seps> must be a string (not a %s)",
(Int)TNAM_OBJ(seps), 0L,
"you can replace <seps> via 'return <seps>;'" );
return FuncSplitStringInternal( self, string, seps, wspace );
}

/* check whether <wspace> is a string */
if ( ! IsStringConv( wspace ) ) {
while ( ! IsStringConv( wspace ) ) {
wspace = ErrorReturnObj(
"SplitString: third argument <wspace> must be a string (not a %s)",
(Int)TNAM_OBJ(wspace), 0L,
"you can replace <wspace> via 'return <wspace>;'" );
return FuncSplitStringInternal( self, string, seps, wspace );
}

/* set SPLITSTRINGSEPS by setting positions of characters in rem to 1 */
Expand Down
Loading

0 comments on commit 329bc6c

Please sign in to comment.