Download cpython_2/environment/repo/Objects/stringlib/find_max_char.h from lolbench26/LoLBench: direct link, hf CLI and curl.
- Browser
- Download file 3.83 kB
-
https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/cpython_2/environment/repo/Objects/stringlib/find_max_char.h
- Command line
-
hf download hf://datasets/lolbench26/LoLBench/cpython_2/environment/repo/Objects/stringlib/find_max_char.h
-
curl -L -o find_max_char.h https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/cpython_2/environment/repo/Objects/stringlib/find_max_char.h
3.83 kB
| /* Finding the optimal width of unicode characters in a buffer */ | |
| /* Mask to quickly check whether a C 'long' contains a | |
| non-ASCII, UTF8-encoded char. */ | |
| Py_LOCAL_INLINE(Py_UCS4) | |
| STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) | |
| { | |
| const unsigned char *p = (const unsigned char *) begin; | |
| const unsigned char *aligned_end = | |
| (const unsigned char *) _Py_ALIGN_DOWN(end, SIZEOF_LONG); | |
| while (p < end) { | |
| if (_Py_IS_ALIGNED(p, SIZEOF_LONG)) { | |
| /* Help register allocation */ | |
| const unsigned char *_p = p; | |
| while (_p < aligned_end) { | |
| unsigned long value = *(const unsigned long *) _p; | |
| if (value & UCS1_ASCII_CHAR_MASK) | |
| return 255; | |
| _p += SIZEOF_LONG; | |
| } | |
| p = _p; | |
| if (p == end) | |
| break; | |
| } | |
| if (*p++ & 0x80) | |
| return 255; | |
| } | |
| return 127; | |
| } | |
| Py_LOCAL_INLINE(Py_UCS4) | |
| STRINGLIB(find_max_char)(const STRINGLIB_CHAR *begin, const STRINGLIB_CHAR *end) | |
| { | |
| const Py_UCS4 mask_limit = MASK_UCS1; | |
| const Py_UCS4 max_char_limit = MAX_CHAR_UCS2; | |
| const Py_UCS4 mask_limit = MASK_UCS2; | |
| const Py_UCS4 max_char_limit = MAX_CHAR_UCS4; | |
| Py_UCS4 mask; | |
| Py_ssize_t n = end - begin; | |
| const STRINGLIB_CHAR *p = begin; | |
| const STRINGLIB_CHAR *unrolled_end = begin + _Py_SIZE_ROUND_DOWN(n, 4); | |
| Py_UCS4 max_char; | |
| max_char = MAX_CHAR_ASCII; | |
| mask = MASK_ASCII; | |
| while (p < unrolled_end) { | |
| STRINGLIB_CHAR bits = p[0] | p[1] | p[2] | p[3]; | |
| if (bits & mask) { | |
| if (mask == mask_limit) { | |
| /* Limit reached */ | |
| return max_char_limit; | |
| } | |
| if (mask == MASK_ASCII) { | |
| max_char = MAX_CHAR_UCS1; | |
| mask = MASK_UCS1; | |
| } | |
| else { | |
| /* mask can't be MASK_UCS2 because of mask_limit above */ | |
| assert(mask == MASK_UCS1); | |
| max_char = MAX_CHAR_UCS2; | |
| mask = MASK_UCS2; | |
| } | |
| /* We check the new mask on the same chars in the next iteration */ | |
| continue; | |
| } | |
| p += 4; | |
| } | |
| while (p < end) { | |
| if (p[0] & mask) { | |
| if (mask == mask_limit) { | |
| /* Limit reached */ | |
| return max_char_limit; | |
| } | |
| if (mask == MASK_ASCII) { | |
| max_char = MAX_CHAR_UCS1; | |
| mask = MASK_UCS1; | |
| } | |
| else { | |
| /* mask can't be MASK_UCS2 because of mask_limit above */ | |
| assert(mask == MASK_UCS1); | |
| max_char = MAX_CHAR_UCS2; | |
| mask = MASK_UCS2; | |
| } | |
| /* We check the new mask on the same chars in the next iteration */ | |
| continue; | |
| } | |
| p++; | |
| } | |
| return max_char; | |
| } | |