16
Utilities / Versatile String Parsing Function by RhoSigma
« on: September 19, 2021, 04:52:02 am »
Versatile String Parsing Function
Author: @RhoSigma
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=4142.0
Version: 2021-08-27
Author's Description:
I guess every developer is sooner or later in need of such a parsing function: Doesn't matter if it's to split a simple text line into its single words, quickly reading CSV data into an array, break up a path specification into the single folder names or get the individual options of a given command line or of an URL query string.
Obviously such a function must be able to recognize several separator chars and needs to be able to suppress the splitting of components in quoted sections. Special to this function is the ability to optionally use different chars for opening quotes and closing quotes, which e.g. allows to read out sections in parenthesis or brackets.
For usage, see the full description available in separate HTML document (compressed file).
Source Code:
Attachments:
Author: @RhoSigma
Source: qb64.org Forum
URL: https://www.qb64.org/forum/index.php?topic=4142.0
Version: 2021-08-27
Author's Description:
I guess every developer is sooner or later in need of such a parsing function: Doesn't matter if it's to split a simple text line into its single words, quickly reading CSV data into an array, break up a path specification into the single folder names or get the individual options of a given command line or of an URL query string.
Obviously such a function must be able to recognize several separator chars and needs to be able to suppress the splitting of components in quoted sections. Special to this function is the ability to optionally use different chars for opening quotes and closing quotes, which e.g. allows to read out sections in parenthesis or brackets.
For usage, see the full description available in separate HTML document (compressed file).
Source Code:
Code: QB64: [Select]
- '--- Full description available in separate HTML document.
- '---------------------------------------------------------------------
- '--- option _explicit requirements ---
- '--- so far return nothing ---
- ParseLine& = -1
- '--- init & check some runtime variables ---
- '--- skip preceding separators ---
- plSkipSepas:
- flag% = 0
- icnt& = icnt& + 1
- '--- redim to clear array on 1st word/component ---
- '--- expand array, if required ---
- plNextWord:
- oaub& = oaub& + 10
- '--- get current word/component until next separator ---
- flag% = 0: nest% = 0: spos& = icnt& - 1
- nest% = 1
- nest% = nest% + 1
- nest% = nest% - 1
- icnt& = icnt& + 1
- epos& = icnt& - 1
- ocnt& = ocnt& + 1
- '--- more words/components following? ---
- '--- final array size adjustment, then exit ---
- plEnd:
- ParseLine& = ocnt& - 1
Attachments: