|
Post by bigbass on Oct 15, 2020 16:49:14 GMT 1
hello vovchik
I didn't see with the very small terminal it was printing 2 spaces yikes! it can be reduced fixed easily though however if we need starting with ^ or ending with $ it's still good to have an example there so I wont edit it (it should have worked also in theory I believe )
'--- this compresses leading or inbetween or trailing spaces to one space DEF FN TRIM$(A1$) = REPLACE$ (A1$ ,"\\s+" , " ",TRUE) PRINT TRIM$(" BaCon is Cool ! ")
these are the same "[[:space:]]+"
OR "\\s+"
\s+ is easier for me to use and remember
OR " ." THE EASIEST
'--- this compresses leading or inbetween or trailing spaces to one space DEF FN TRIM$(A1$) = REPLACE$ (A1$ ," .", " ",1) PRINT TRIM$(" BaCon is Cool ! ")
P.S I will in another place port some common javascript commands to bacon javasscipt to bacon is fun exercise
|
|
|
Post by ptitjoz on Oct 16, 2020 14:16:46 GMT 1
Hello thank you to everyone for their contribution. it is not the method that is missing! it would be nice if a SUPPRESPACE$() function could be integrated natively in BACON.
Best regards
|
|
|
Post by bigbass on Oct 16, 2020 21:49:00 GMT 1
Just to document a few things so I don't forget How this is done in other languages as a reference (in BASIC also) rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tailHow it is done in bacon www.basic-converter.org/documentation.html#CHOP$' 0 means on both sides, 1 means chop at the left and 2 means chop at the right. Examples: We don't have an official rosettacode example yet so here we go following the pattern given '---Remove leading whitespace PRINT CHOP$(" String with spaces "," ",1)
'---Remove trailing whitespace PRINT CHOP$(" String with spaces "," ",2)
'---Remove both leading and trailing whitespace PRINT CHOP$(" String with spaces "," ",0)
|
|
|
Post by bigbass on Nov 1, 2020 19:45:09 GMT 1
|
|
|
Post by rikky on Feb 7, 2021 13:25:00 GMT 1
This is why I have so much troubles with BASH It is already difficult to read and understand, but when You finally get the hang of it. .... Then it isn't even true. I thought this would work: DEF FN SINGLESPACE$(string$) = REPLACE$(string$,"[[:space:]]+"," ", TRUE)
for [:space:] means space and [[:space:]]+ means a lot af spaces. but apparently it also means a newline. DEF FN SINGLESPACE$(string$) = REPLACE$(string$,"[[:space:]]+"," ", TRUE)
string$ = "bla bla bla" PRINT SINGLESPACE$(string$) result: bla bla bla
but now: DEF FN SINGLESPACE$(string$) = REPLACE$(string$,"[[:space:]]+"," ", TRUE)
string$ = "bla bla bla" & NL$ & "bloep bloep bloep" PRINT SINGLESPACE$(string$)
result: bla bla bla bloep bloep bloep Rik.
|
|
|
Post by Pjot on Feb 7, 2021 14:02:50 GMT 1
Hi rik, That's right, the [[:space:]] class means more than only space, it is defined as [ \t\r\n\v\f]. In your case, you probably better use [[:blank:]] which is a space and a tab. See also this url for more information. HTH Peter
|
|
|
Post by bigbass on Feb 11, 2021 19:12:20 GMT 1
my original c port stopped working first post I fixed it again (no problem ) and improved it still a mystery for me why I need a homemade NULLFIX for it to work maybe a raspberry pi3 thing? if you use just NULL instead of NULLFIX you will see what I mean any questions I will post pure c working code basic-converter.proboards.com/post/12781to get to the point in C an array can be tested equal to NULL but not in bacon we have to use the NULLFIX workaround now working again '--- code ported and fixed to work for bacon by bigbass '--- tested with gcc and clang
'--- code removes extra spaces in strings '--- this is very important when handling arguments as input PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-pointer-to-int-cast
'---porting c code fixes for bacon '---this also fixes syntax highlighting errors for single quotes '---converted it to a bacon function '======================================== FUNCTION REMOVE_EXTRA_SPACES(char text[]) TYPE long int '========================================
LOCAL c = 0 TYPE int LOCAL d = 0 TYPE int DECLARE blank[1000] TYPE char LOCAL temp TYPE int
'---pointer to int cast fix for NULL NULLFIX = (int) '\x0' SPACE = '\x20'
WHILE text[c] != NULLFIX IF text[c] == SPACE THEN temp = c + 1 IF text[temp] != NULLFIX THEN WHILE text[temp] == SPACE AND text[temp] != NULLFIX IF text[temp] == SPACE THEN INCR c ENDIF INCR temp WEND ENDIF ENDIF blank[d] = text[c] INCR c INCR d WEND blank[d] = NULLFIX RETURN (int) blank END FUNCTION
PREPARSED = REMOVE_EXTRA_SPACES("Another char array with too many spaces ")
PRINT "preparsed " ,PREPARSED FORMAT "%s%s "
|
|
|
Post by vovchik on Feb 12, 2021 1:29:18 GMT 1
Dear Joe,
Thanks. It works fine. I had to add:
-Wno-pointer-to-int-cast
to PRAGMA OPTIONS on Mint (64-bit) to avoid a warning.
With kind regards, vovchik
|
|
|
Post by bigbass on Feb 12, 2021 3:33:34 GMT 1
Hello vovchik
thanks for testing the first time you tried this it seg faulted
it is good it will work for you with this option -Wno-pointer-to-int-cast
I could quiet it with that also on the RPI3
PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-pointer-to-int-cast
I used '---pointer to int cast fix for NULL NULLFIX = (int) '\x0'
as a workaround
good to see it works 64 bit also
I know this is low level looking code (not pretty ) but it is important to port these things to get the bacon syntax right and it shows what's really going on
I will mod the code so it works 32 and 64 bit THANKS
Joe
|
|