faster implementation than AsciiStrnCmp. AsciiStrnCmp calls AsciiStrSize 4 times. int strncmp( const char * s1, const char * s2, size_t n ) { while ( n && *s1 && ( *s1 == *s2 ) ) { ++s1; ++s2; --n; } if ( n == 0 ) { return 0; } else { return ( *(unsigned char *)s1 - *(unsigned char *)s2 ); } } Compare no more than N characters of S1 and S2, returning less than, equal to or greater than zero
| 71 | if S1 is lexicographically less than, equal to or |
| 72 | greater than S2. */ |
| 73 | int |
| 74 | strncmp(const char *s1, const char *s2, size_t n) |
| 75 | { |
| 76 | unsigned char c1 = '\0'; |
| 77 | unsigned char c2 = '\0'; |
| 78 | |
| 79 | if (n >= 4) |
| 80 | { |
| 81 | size_t n4 = n >> 2; |
| 82 | do |
| 83 | { |
| 84 | c1 = (unsigned char) *s1++; |
| 85 | c2 = (unsigned char) *s2++; |
| 86 | if (c1 == '\0' || c1 != c2) |
| 87 | return c1 - c2; |
| 88 | c1 = (unsigned char) *s1++; |
| 89 | c2 = (unsigned char) *s2++; |
| 90 | if (c1 == '\0' || c1 != c2) |
| 91 | return c1 - c2; |
| 92 | c1 = (unsigned char) *s1++; |
| 93 | c2 = (unsigned char) *s2++; |
| 94 | if (c1 == '\0' || c1 != c2) |
| 95 | return c1 - c2; |
| 96 | c1 = (unsigned char) *s1++; |
| 97 | c2 = (unsigned char) *s2++; |
| 98 | if (c1 == '\0' || c1 != c2) |
| 99 | return c1 - c2; |
| 100 | } while (--n4 > 0); |
| 101 | n &= 3; |
| 102 | } |
| 103 | |
| 104 | while (n > 0) |
| 105 | { |
| 106 | c1 = (unsigned char) *s1++; |
| 107 | c2 = (unsigned char) *s2++; |
| 108 | if (c1 == '\0' || c1 != c2) |
| 109 | return c1 - c2; |
| 110 | n--; |
| 111 | } |
| 112 | |
| 113 | return c1 - c2; |
| 114 | } |
| 115 | |
| 116 | |
| 117 |
no outgoing calls