Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
FindFirstIn
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Ben Bacarisse" data-source="post: 5161223"><p>Yes. You could write</p><p></p><p>bool table[256];</p><p>bool *tab = table - CHAR_MIN;</p><p></p><p>(I used bool because that's logically the right type and you are a fan</p><p>of C99, but there might be a reason you chose char.)</p><p></p><p>You should certainly swap which pointer you do *(unsigned char *) on.</p><p>If you make a local unsigned char *pset, change p to be char * and use</p><p>the cast on tab[*p] instead, you get far fewer type mismatches with</p><p>pretty much the same code complexity. (And you can remove c, it's not</p><p>used.)</p><p></p><p>But, since you return a pointer to the null byte when none of the</p><p>non-null characters is found, logically the null is in the set of</p><p>searched-for characters. The code gets a lot simpler if you treat the</p><p>null just like the others:</p><p></p><p>bool table[256] = {0};</p><p>bool *tab = table - CHAR_MIN;</p><p>do tab[*set] = true; while (*set++);</p><p></p><p>while (!tab[*str]) ++str;</p><p>return str;</p><p></p><p>gcc compiles this to 20/21 instructions (depending on optimising for</p><p>speed or size). That's fewer than your hand-coded assembler version,</p><p>though it may, of course, be either more bytes, or slower, or both.</p></blockquote><p></p>
[QUOTE="Ben Bacarisse, post: 5161223"] Yes. You could write bool table[256]; bool *tab = table - CHAR_MIN; (I used bool because that's logically the right type and you are a fan of C99, but there might be a reason you chose char.) You should certainly swap which pointer you do *(unsigned char *) on. If you make a local unsigned char *pset, change p to be char * and use the cast on tab[*p] instead, you get far fewer type mismatches with pretty much the same code complexity. (And you can remove c, it's not used.) But, since you return a pointer to the null byte when none of the non-null characters is found, logically the null is in the set of searched-for characters. The code gets a lot simpler if you treat the null just like the others: bool table[256] = {0}; bool *tab = table - CHAR_MIN; do tab[*set] = true; while (*set++); while (!tab[*str]) ++str; return str; gcc compiles this to 20/21 instructions (depending on optimising for speed or size). That's fewer than your hand-coded assembler version, though it may, of course, be either more bytes, or slower, or both. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
FindFirstIn
Top