398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
# File 'ext/sin_fast_blank/sin_fast_blank.c', line 398
static VALUE rb_str_blank(VALUE str) {
long len = RSTRING_LEN(str);
if (len == 0) return Qtrue;
const unsigned char* ptr = (const unsigned char*)RSTRING_PTR(str);
const unsigned char* end = ptr + len;
rb_encoding* enc = STR_ENC_GET(str);
bool asciicompat = rb_enc_asciicompat(enc) != 0;
if (asciicompat) {
const unsigned char* non_ascii_pos = NULL;
if (check_blank(ptr, (size_t)len, &non_ascii_pos)) return Qtrue;
if (non_ascii_pos == NULL) return Qfalse;
ptr = non_ascii_pos;
}
bool is_unicode = is_unicode_encoding(enc);
while (ptr < end) {
int clen = rb_enc_precise_mbclen((const char*)ptr, (const char*)end, enc);
if (!MBCLEN_CHARFOUND_P(clen)) return blank_undecodable(str, enc);
unsigned int codepoint = rb_enc_mbc_to_codepoint((const char*)ptr, (const char*)end, enc);
if (!is_blank_codepoint(codepoint, enc, is_unicode)) return Qfalse;
ptr += MBCLEN_CHARFOUND_LEN(clen);
/*
* An ASCII run starts here, so hand it back to the SIMD scan instead of decoding it a character at a time. Only an ASCII-compatible
* encoding may do this: anywhere else a byte below 0x80 is not a character on its own.
*
* Resuming the decode afterwards is safe too. The scan only ever hands back a position holding a byte of 0x80 or above, and every
* byte it passed was a single-byte blank, so the decode restarts on a character boundary. A non-blank ASCII byte settles the answer
* outright and leaves no position to hand back.
*/
if (asciicompat && ptr < end && *ptr < 0x80) {
const unsigned char* non_ascii_pos = NULL;
if (check_blank(ptr, (size_t)(end - ptr), &non_ascii_pos)) return Qtrue;
if (non_ascii_pos == NULL) return Qfalse;
ptr = non_ascii_pos;
}
}
return Qtrue;
}
|