Advertisement
Ladies_Man

#NUP rk2 wcsCspn_wcsspn

Feb 26th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wchar.h>
  4. #include <string.h>
  5.  
  6.  
  7. //original idea by Sergey G. (c)
  8.  
  9. size_t x_wcscspn(char *s, char *reject)
  10. {
  11.     if (!s || !(*s) || !reject || !(*reject)) return strlen(s);
  12.     int len = strlen(reject), len2 = strlen(s);
  13.     char* temp;
  14.     char* one = s;
  15.     int templen;
  16.     do {
  17.         __asm__ __volatile__ ("repne scasb \n\t" : "=c"(templen), "=D"(temp) : "a"(*s), "D"(reject), "c"(len));
  18.         __asm__ __volatile__  goto("jz %l[s_ret] \n\t" : : : "cc" : s_ret);
  19.     } while (*(++s));
  20.     s = 0;
  21. s_ret:
  22.     if (s == 0) return len2;
  23.     return s - one;
  24. }
  25.  
  26. size_t x_wcsspn(wchar_t *s, wchar_t *accept)
  27. {
  28.     if (!s || !(*s) || !accept || !(*accept)) return wcslen(s);
  29.     int templen, len = wcslen(accept), len2 = wcslen(s);
  30.     wchar_t* temp;
  31.     wchar_t* start = s;
  32.     //this works on Win only. in Linux version replace "scasw" with "scasl"
  33.     do {    __asm__ __volatile__ ("repne scasw \n\t" : "=c"(templen), "=D"(temp) : "a"(*s), "D"(accept), "c"(len));
  34.             __asm__ __volatile__  goto("jnz %l[exit] \n\t" : : : "cc" : exit);
  35.     } while (*(++s));
  36.  
  37.     s = 0;
  38.     exit:
  39.     if (s == 0) return len2;
  40.     return (s - start);
  41. }
  42.  
  43. /*asm [volatile] ( AssemblerTemplate
  44.                       : OutputOperands
  45.                       [ : InputOperands
  46.                       [ : Clobbers ] ])
  47.  
  48.      asm [volatile] goto ( AssemblerTemplate
  49.                            :
  50.                            : InputOperands
  51.                            : Clobbers
  52.                            : GotoLabels)*/
  53.  
  54. //"cc"
  55. //  The "cc" clobber indicates that the assembler code modifies the flags register. On some machines, GCC represents the condition codes as a specific hardware register; "cc" serves to name this register. On other machines, condition code handling is different, and specifying "cc" has no effect. But it is valid no matter what the target.
  56.  
  57. int main()
  58. {
  59.     char str[10] = "abcddddef";
  60.     wchar_t wstr[10] = L"abcddddef";
  61.     char rej[10] = "efhg";
  62.     wchar_t acc[10] = L"cabd";
  63.     int ans;
  64.     ans = x_wcscspn(str, rej);      //CHAR
  65.     printf("wcsCspn=%d\n", ans);
  66.     ans = x_wcsspn(wstr, acc);      //WCHAR_T
  67.     printf("wcsspn=%d\n", ans);
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement