Closed
Conversation
Contributor
|
Instead of returning a dynamic array that will need to be freed, what about something along the lines of #define NOBDEF static inline
#define NOB_STRIP_PREFIX
#define NOB_IMPLEMENTATION
#include "nob.h"
NOBDEF Nob_String_View nob_sv_splitline(Nob_String_View *sv) {
Nob_String_View line = sv_chop_by_delim(sv, '\n');
if (line.count && line.data[line.count - 1] == '\r')
line.count--;
return line;
}
// Nob_String_View *sv, Nob_String_View line
#define nob_sv_split_by_line(sv, line) \
for (Nob_String_View (line) = nob_sv_splitline((sv)); (sv)->count || (line).count; (line) = nob_sv_splitline((sv)))
int main(void) {
int result = 0;
String_Builder sb = {};
if (!read_entire_file(__FILE__, &sb))
return_defer(1);
String_View sv = sb_to_sv(sb);
nob_sv_split_by_line(&sv, sv2) {
nob_log(INFO, "\"" SV_Fmt "\"", SV_Arg(sv2)); //
}
defer:
sb_free(sb);
return result;
} |
Contributor
Author
|
First of all, thanks for taking the time to have a well thought out suggestion. If common lexing tasks would benefit from having the data/count info around for a bit, then having a How about only adding sv_split_line, and leaving it up to the user to make their own dynamic array or macro depending on the use-case? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I'm hoping the title gives it away, but if not, see corresponding test for usage.
Notes on this commit:
String_Viewsis a dynamic array of itemsString_View, following typical nob dynamic array conventions