|
Post by bigbass on Mar 11, 2020 20:06:59 GMT 1
Hello guys
(vovchik this may be useful for bish ) and its a function already
the terminal can filter out whitespace but when we need to filter plain text its a problem
there were several obstacles to overcome to get it to compile and run in bacon added a baconized NULLFIX and a SPACE variable
Joe
'--- 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 -Wformat=0
'---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 "
terminal out
preparsed Another char array with too many spaces
|
|
|
Post by vovchik on Mar 11, 2020 20:35:13 GMT 1
Dear Joe, Thanks. I will start testing. I have a few such functions in bish already (CRUNCH, PURGE and SPC_RM), and another won't hurt. There are always cases were one is better than another. With kind regards, vovchik PS. Testing in PI4 now and getting a runtime segfault. Will investigate why...
|
|
|
Post by bigbass on Mar 12, 2020 5:05:33 GMT 1
Thanks for trying
when something doesnt work as planned I usually try pure c or c++ first if that works then I move to porting and testing the results are the same
the reason I said that is if the pure c code doesnt work on the pi4 then it would be much less likely the ported code would work
I changed from gets to fgets (to update this old demo) then modded it for what I wanted to do
here is the source in c should have posted it before as reference above
Joe
Ps if you see what went wrong let me know I m looking to buy a pi4 also
#include <stdio.h>
int main() { char text[1000], blank[1000]; int c = 0, d = 0; printf("Enter some text\n"); //gets(text); //gives errors fgets( text, sizeof(text), stdin ); // safe to use while (text[c] != '\0') { if (text[c] == ' ') { int temp = c + 1; if (text[temp] != '\0') { while (text[temp] == ' ' && text[temp] != '\0') { if (text[temp] == ' ') { c++; } temp++; } } } blank[d] = text[c]; c++; d++; } blank[d] = '\0'; printf("Text after removing blanks\n%s\n", blank); return 0; }
|
|
|
Post by rikky on Mar 12, 2020 8:48:05 GMT 1
What's wrong with FUNCTION REMOVE_EXTRA_SPACES$(string$)
WHILE INSTR(string$," ") string$ = REPLACE$(string$," "," ") WEND RETURN string$
END FUNCTION
PRINT REMOVE_EXTRA_SPACES$("BaCon is Cool !") Rik.
|
|
|
Post by vovchik on Mar 12, 2020 13:39:01 GMT 1
Dear Joe and Rik, Thanks guys. Rik's solution is nice and simple. I think I should plop it into bish, but give the user the opportunity to select the "garbage char or chars". Maybe like this: OPTION EXPLICIT TRUE OPTION UTF8 TRUE
DECLARE dummy$ = "1asdf asdf" & NL$ & NL$ & "2asdf asdf" & NL$ & \ "3asdf asdf" & NL$ & NL$ & NL$ & NL$ & "4asdf asdf"
' ------------------ FUNCTION RM_EXTRA$(STRING org$, STRING extra$) ' ------------------ ' Return string (arg1) from which consecutive chars (arg2), ' have been removed, except for a single instance. WHILE INSTR(org$, extra$ & extra$) org$ = REPLACE$(org$, extra$ & extra$, extra$) WEND RETURN org$ END FUNCTION
PRINT RM_EXTRA$("BaCon is C", " ") PRINT "---" PRINT RM_EXTRA$("BaCon is C", " ") PRINT "---" PRINT RM_EXTRA$("BaCon |||| is C", "|") PRINT "---" PRINT dummy$ PRINT "---" PRINT RM_EXTRA$(dummy$, NL$) PRINT "---" PRINT RM_EXTRA$(dummy$, NL$ & NL$) What do you think? With kind regards, vovchik PS. Now in bish as RM_EXTRA. See first post under bish.
|
|
|
Post by Pjot on Mar 12, 2020 20:08:23 GMT 1
How about this: k$ = "Another char array with too many spaces "
PRINT REPLACE$(k$, "[[:space:]]+", " ", TRUE)
See also here. BR Peter
|
|
|
Post by vovchik on Mar 12, 2020 20:50:32 GMT 1
Dear Peter,
Thanks. That is very terse and efficient. What about this in bish, to have the best of both worlds, and to handle multiple extra chars?
' ------------------ FUNCTION RM_EXTRA$(STRING org$, STRING extra$) ' ------------------ ' Return string (arg1) from which consecutive chars (arg2), ' have been removed, except for a single instance. IF extra$ = " " THEN org$ = REPLACE$(org$, "[[:space:]]+", " ", TRUE) ELSE WHILE INSTR(org$, extra$ & extra$) org$ = REPLACE$(org$, extra$ & extra$, extra$) WEND END IF RETURN org$ END FUNCTION
With kind regards, vovchik
|
|
|
Post by Pjot on Mar 13, 2020 9:03:21 GMT 1
Hi vovchik,
It depends on what you want to achieve. The functions mentioned here always rip all extra spaces from everything. But what if we look at the following line:
k$ = "He said that \"there are too many spaces\" in the line "
If you want to preserve the actual quote then it might be better to use delimited string functions.
You can see the difference:
OPTION COLLAPSE TRUE PRINT COIL$(AMOUNT(k$), TOKEN$(k$, _))
PRINT REPLACE$(k$, "[[:space:]]+", " ", TRUE)
Best regards Peter
|
|
|
Post by bigbass on Mar 13, 2020 18:01:33 GMT 1
Hello Thanks everyone
vovchik ,riky ,Peter
all feedback is welcomed
I just wrote a long post and lost it by dragging the mouse ahhh
but the idea was
regx is the way I need to solve the parsing problem for more things Im doing behind the scenes that I didnt explain
and the other examples are short and good for removing spaces and repeating characters which are also very useful
thanks guys Joe
|
|
|
Post by ptitjoz on Oct 9, 2020 11:04:27 GMT 1
What's wrong with FUNCTION REMOVE_EXTRA_SPACES$(string$)
WHILE INSTR(string$," ") string$ = REPLACE$(string$," "," ") WEND RETURN string$
END FUNCTION
PRINT REMOVE_EXTRA_SPACES$("BaCon is Cool !") Rik. Hello ! I really like the function you offer. However, it should not remove the space at the start and end of the string. In Excel or Libreoffice there is the function = SUPPRESPACE (A1) which does this very well. Couldn't we natively implement a similar function in Bacon?
|
|
|
Post by rikky on Oct 10, 2020 6:17:49 GMT 1
FUNCTION REMOVE_EXTRA_SPACES$(string$)
LOCAL midstring$ = MID$(string$,2,LEN(string$)-2)
WHILE INSTR(midstring$," ") midstring$ = REPLACE$(midstring$," "," ") WEND RETURN LEFT$(string$,1) & midstring$ & RIGHT$(string$,1)
END FUNCTION
PRINT "***" & REMOVE_EXTRA_SPACES$(" BaCon is Cool ! ") & "***" Rik.
|
|
|
Post by vovchik on Oct 10, 2020 23:21:07 GMT 1
Dear Rik,
I have something similar in bish, except that it offers a bit more functionality:
' ------------------ FUNCTION RM_EXTRA$(STRING org$, STRING extra$) ' ------------------ ' Return string (arg1) from which consecutive chars (arg2), ' have been removed, except for a single instance. IF extra$ = " " OR extra$ = "" THEN org$ = REPLACE$(org$, "[[:space:]]+", " ", TRUE) ELSE WHILE INSTR(org$, extra$ & extra$) org$ = REPLACE$(org$, extra$ & extra$, extra$) WEND END IF RETURN org$ END FUNCTION
PRINT "***" & RM_EXTRA$(" BaCon is Cool ! ", " ") & "***"
With kind regards, vovchik
|
|
|
Post by Pjot on Oct 11, 2020 10:56:23 GMT 1
Hi ptitjoz, That's interesting. The code from rikky and vovchik will work of course. Conceptually, what you're asking for is like an inverse CHOP$ function: instead of removing redundant whitespaces at the edges of the string, you want to remove them 'inside' the string? I tried to lookup documentation for the 'SUPPRESPACE' function. If this is the functionality you are looking for, then it can be achieved as follows: PRINT CHOP$(REPLACE$(k$, "[[:space:]]+", " ", TRUE))
Or with OPTION COLLAPSE and COIL$ (needs latest beta): OPTION COLLAPSE TRUE PRINT COIL$(AMOUNT(k$), TOKEN$(k$, _))
Cordialement, Peter
|
|
|
Post by bigbass on Oct 14, 2020 17:16:53 GMT 1
Hello everyone offered quality working answers thanks guys we can always solve the same problem different ways so why not another spin to keep the wheel going I needed something like this myself yesterday javascript uses a lot of regex and was wondering how to do it bacon Joe '--- how you could use regex and DEF FN to make your own specialized Macros
DEF FN SUPPRESPACE$(A1$) = CHOP$(REPLACE$ (A1$ ,"\\s+" , " ",TRUE)) PRINT SUPPRESPACE$(" BaCon is Cool ! ")
|
|
|
Post by vovchik on Oct 14, 2020 21:51:51 GMT 1
Dear Joe, Nice and terse. Thanks! With kind regards, vovchik
|
|