home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,311 of 243,242   
   Bonita Montero to All   
   Re: strspn()   
   02 Dec 25 13:44:59   
   
   From: Bonita.Montero@gmail.com   
      
   Am 22.11.2025 um 01:04 schrieb bart:   
   > On 21/11/2025 18:41, Michael Sanders wrote:   
   >> So strspn, perfect for what I need!   
   >>   
   >> before:   
   >>   
   >> int ok = 1;   
   >>   
   >> if (strlen(colorSpec) == 4) {   
   >>      for (int i = 0; i < 4; i++) {   
   >>          char c = colorSpec[i];   
   >>          if (c < '1' || c > '8') { ok = 0; break; }   
   >>      }   
   >> } else ok = 0;   
   >>   
   >> after:   
   >>   
   >> strspn() returns the length of the leading substring   
   >> consisting ONLY of characters in the allowed set...   
   >>   
   >> if (strlen(colorSpec) != 4 && strspn(colorSpec, "12345678") = 4) FAIL;   
   >>   
   >   
   > Performance probably isn't critical in your use-case, but the solution   
   > using 'strspn' was three times as slow as doing the explicit code.   
   > (Test string was a fixed "8725" and tested 100M times.)   
   Try that with AVX. Make a 256 bit vector with 0's and a vector with '9's.   
   Have two compares resulting in a 32 * 8 bit 0 / 0xFF mask. Convert that   
   mask to a bitmask (with AVX-512 the comparison directly results in a   
   bitmask, i.e. no conversion is necessary).   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca