Text Parser - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Utilities (https://staging.qb64phoenix.com/forumdisplay.php?fid=8) +---- Thread: Text Parser (/showthread.php?tid=942) |
Text Parser - TerryRitchie - 10-03-2022 UPDATE: I made changes to the code with great suggestions and code examples from Pete. The routine is much more stable now. Thanks Pete! By the way - writing text parsing routines is much harder than it looks. So much to consider in their design. While writing the library for lesson 20 of the tutorial I wrote a text parser that's fairly efficient. I needed a parsing routine that reported the number of lines the input text string would be parsed to and could deliver one line of text at a time on demand. This is what I came up with: Code: (Select All) FUNCTION ParseText$ (TextIn AS STRING, MaxWidth AS INTEGER, Action AS INTEGER) STATIC Drop the function into your own code and parse away. RE: Text Parser - mnrvovrfc - 10-03-2022 Thank you for this. However, "Action" is not necessary and in fact produces confusion. To actually get lines must set it to zero, so why even need this parameter? For more serious work (eg. include into a simple word processor) check for hyphenated words as well as spaces to split lines. RE: Text Parser - TerryRitchie - 10-03-2022 (10-03-2022, 12:49 AM)mnrvovrfc Wrote: Thank you for this. However, "Action" is not necessary and in fact produces confusion. To actually get lines must set it to zero, so why even need this parameter? For more serious work (eg. include into a simple word processor) check for hyphenated words as well as spaces to split lines. Oh sure, the action parameter was added for my library needs. Strip the action code out if all you need is a simple parser with the ability to report one line at time during subsequent calls. The library I'm writing has the need for number of lines to be reported. Pretty handy. Here's an example use: t$ = "big long crazy text string here. blah blah blah blah ... and blah" FOR x=1 TO VAL(ParseText$(t$, 40, 1)) Array(x).text = ParseText$(t$, 40, 0) NEXT x Or in my library's case I need to know the number of lines the text will be placed in to size ASCII text box windows appropriately. Box(Index).height = VAL(ParseText$(t$, 40, 1)) + 4 ' top border + header text + header line delimiter + border bottom + lines of text 'Then, as the box is drawn lines of parsed text can be brought in one at time as needed "as well as spaces to split lines" It does do that. RE: Text Parser - RhoSigma - 10-03-2022 @TerryRitchie , did you had a look on this parsing function, giving the right parameters it's able to do the task, not only line parsing is possible and it will return the number of parsed components. RE: Text Parser - Pete - 10-03-2022 I have one of these WP routines that gets rid of unwanted text characters in SCREEN 0. I call it, Parsing My ASCII Off.bas Pete - There's always a party somewhere, and I went to way too many of them. RE: Text Parser - James D Jarvis - 10-03-2022 This crashes the program if there are semicolons in the input string on my windows machine. RE: Text Parser - RhoSigma - 10-03-2022 (10-03-2022, 11:35 AM)James D Jarvis Wrote: This crashes the program if there are semicolons in the input string on my windows machine. You've to be more specific what crashes, Terry's routine, my routine or Pete's? Well could imagine Pete's routine is supposed to crash, I'd bet my ass semicolon is an "unwanted" char for him on SCREEN 0 RE: Text Parser - TerryRitchie - 10-03-2022 (10-03-2022, 11:35 AM)James D Jarvis Wrote: This crashes the program if there are semicolons in the input string on my windows machine. Yep, creating a text parser is hard, a lot harder than I originally thought it would be. Pete got hold of me last night to point out some improvements that can be made. I just got up. I'll make those changes and upload the results in a bit. RE: Text Parser - mnrvovrfc - 10-03-2022 (10-03-2022, 11:35 AM)James D Jarvis Wrote: This crashes the program if there are semicolons in the input string on my windows machine.You'll have to post your example. I've tried something with at least two semicolons on Terry's function and it works just fine. I assume you mean Terry's function because it's why this thread was started. I scanned the source code on the first post and there doesn't seem to be a special check for a character other than CHR$(32) space. RE: Text Parser - TerryRitchie - 10-03-2022 Ok, I studied Pete's suggestions and added his code to the routine. Much, much, better. Thanks Pete! The code in the original post above has been updated. |