Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/sin_fast_blank.rb

Instance Method Summary collapse

Instance Method Details

#ascii_blank?Boolean

Returns:

  • (Boolean)


445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'ext/sin_fast_blank/sin_fast_blank.c', line 445

static VALUE rb_str_ascii_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);

  /*
   * This one never raises, because bytes that decode to nothing are no more an ASCII blank than the characters they failed to form.
   * An ASCII-compatible encoding does not even need the decoder: a byte of 0x80 or above only ever starts a character whose codepoint
   * is 0x80 or above there, so the first one settles the answer.
   */
  if (rb_enc_asciicompat(enc)) return check_ascii_blank(ptr, (size_t)len) ? Qtrue : Qfalse;

  while (ptr < end) {
    int clen = rb_enc_precise_mbclen((const char*)ptr, (const char*)end, enc);
    if (!MBCLEN_CHARFOUND_P(clen)) return Qfalse;
    unsigned int codepoint = rb_enc_mbc_to_codepoint((const char*)ptr, (const char*)end, enc);
    if (codepoint != 0 && !rb_isspace(codepoint)) return Qfalse;
    ptr += MBCLEN_CHARFOUND_LEN(clen);
  }

  return Qtrue;
}

#blank?Boolean

Returns:

  • (Boolean)


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;
}