Say I have a string "text", a caret position "caret" and then want to find the current word (seperated by space).
The way I'm currently doing it seems inefficient and I was wondering if anyone had a efficient way of doing it?
const char* text;
int caret;
int initpos;
int start;
int count = 0;
char word[256];
// text and caret values assigned here.
initpos = caret;
while(caret > 0 && text[caret] != ' ') // get start
{
caret--;
count++;
}
start = caret;
caret = initpos;
while(text[caret] && text[caret] != ' ') // get end
{
caret++;
count++;
}
word = strsub(text, start, count);
By "seems inefficient", do you mean the code looks inefficient to you or that you've measured and found it too slow for you purposes?
Your method takes O(n) steps where n is the length of the longest word in your input. That's pretty fast unless your words have the size of DNA strings.
A faster method, for some datasets, would be to use an index of word start and end positions. An binary search tree storing intervals would fit this bill, but at the expense of O(lg N) retrieval time, where N is the number of words in your input. Probably not worth it.