Trim.right()?

BlitzMax Forums/BlitzMax Beginners Area/Trim.right()?

Grisu(Posted 2007) [#1]
Hello!

Is it possible to tell the trim() function to remove the unprintable characters only from the right side of a string, leaving the rest of it intact?

Example:
#Tab# #Tab# Text ########

becomes:
#Tab# #Tab# Text

and not:
Text

Thanks, Grisu


Dreamora(Posted 2007) [#2]
no, not that I know off
If you want to do so, split the string up, trim the right side and combine it again ...

Or write a function that just searches mid from string.length with step -1 and stops at the first alphanumeric char and then returns left up to that point


Brucey(Posted 2007) [#3]
No, but you can write your own...

Either in native BlitzMax, or ripping the guts out of what's already there...

blitz_string2.c
#include "blitz.h"

BBString *RightTrim( BBString *str ){
	int b=0,e=str->length;
	if( b==e ) return &bbEmptyString;
	while( str->buf[e-1]<=' ' ) --e;
	if( e-b==str->length ) return str;
	return bbStringFromShorts( str->buf+b,e-b );
}

BBString *LeftTrim( BBString *str ){
	int b=0,e=str->length;
	while( b<e && str->buf[b]<=' ' ) ++b;
	if( b==e ) return &bbEmptyString;
	if( e-b==str->length ) return str;
	return bbStringFromShorts( str->buf+b,e-b );
}


string_test.bmx
SuperStrict

Import BRL.Blitz
Import BRL.StandardIO

Import "blitz_string2.c"

Extern
	Function RightTrim:String(s:String)
	Function LeftTrim:String(s:String)
End Extern


Print "." + RightTrim("   a c b d   ") + "."
Print "." + RightTrim("   a c c d~t~t ") + "."
Print "." + LeftTrim("   a c b d   ") + "."
Print "." + LeftTrim(" ~t~ta c c d~t~t ") + "."


:-p


Grisu(Posted 2007) [#4]
Thanks for the help guys!

@Brucey:
Compiling:blitz_string2.c
In file included from C:/Program Files/BlitzMax/mod/brl.mod/blitz.mod/blitz.h:5,
from E:/DFA/blitz_string2.c:1:
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:5:19: ctype.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:6:19: stdio.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:7:20: string.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:8:20: stdlib.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:9:20: stdarg.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:10:20: assert.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:11:22: sys/stat.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:12:18: time.h: No such file or directory
C:/Program Files/BlitzMax/mod/pub.mod/stdc.mod/stdc.h:24:21: direct.h: No such file or directory
Build Error: failed to compile E:/DFA/blitz_string2.c

Am I missing files???


Zethrax(Posted 2007) [#5]
Here's a couple of functions I knocked up, if they're any good to you.


Print ">" + RightTrim( "   abc   " ) + "<"
Print ">" + LeftTrim( "   abc   " ) + "<"

End

Function RightTrim:String( value:String )
	For Local pos:Int = value.length - 1 To 0 Step -1
		If Asc( value[ pos..pos + 1 ] ) > 32
			value = value[ ..pos + 1 ]
			Exit
		EndIf
	Next
	Return value
End Function


Function LeftTrim:String( value:String )
	For Local pos:Int = 0 To value.length - 1
		If Asc( value[ pos..pos + 1 ] ) > 32
			value = value[ pos.. ]
			Exit
		EndIf
	Next
	Return value
End Function




Grisu(Posted 2007) [#6]
Thanks Bill... works out of the box.


Brucey(Posted 2007) [#7]
Am I missing files??

Yeah... MinGW, by the looks of it.

You'll find the C functions significantly faster than the equivalent BlitzMax implementation...