Created
February 1, 2018 16:09
-
-
Save gpertea/39141a105a42a0a78374254f31dc5728 to your computer and use it in GitHub Desktop.
fast in-place parse of a list of comma-delimited int values in a string SAM tag
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
char* str=brec->tag_str("ZD"); //let's say the tag is "ZD" | |
GVec<int> vals; | |
char* p=str; //slice start | |
for (int i=0;;++i) { | |
char ch=str[i]; | |
if (ch==',') { | |
str[i]=0; | |
int v=atoi(p); //check for int parsing errors? | |
vals.Add(v); | |
p=str+i+1; | |
} else if (ch==0) { | |
int v=atoi(p); //check for int parsing errors? | |
vals.Add(v); | |
break; | |
} | |
} | |
//now vals should have all parsed int values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wondering if, for longer strings, using strchr is faster (if it's implemented with SSE optimization). In that case the code would look like this:
In the case of multiple choices of a single-character delimiter, the code is very similar, using
strpbrk(p, delim)
instead of strchr(), wheredelim
is a string with the set of characters which can be slice delimiters (e.g.char* delim=",;.:";
).If the delimiter is a string using strstr() is definitely the way to go (especially if it's SSE optimized, which it should be):