Module: Mingle::Chars

Defined in:
lib/mingle.rb

Constant Summary collapse

SIMPLE_ESCAPE_VALS =
"\n\r\t\f\b"
SIMPLE_ESCAPE_STRS =
'\n\r\t\f\b'

Class Method Summary collapse

Class Method Details

.ctl_char?(ch) ⇒ Boolean

Returns:

  • (Boolean)


528
529
530
# File 'lib/mingle.rb', line 528

def ctl_char?( ch )
    ( 0x00 ... 0x20 ).include?( ch.ord )
end

.external_form_of(val) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/mingle.rb', line 541

def external_form_of( val )

    res = RubyVersions.when_19x( '"' ) { |s| s.encode!( "binary" ) }

    val.each_byte do |b|
        
        case
        when Chars.ctl_char?( b )
            if s = Chars.get_simple_escape( b )
                res << s
            else
                res << sprintf( "\\u%04X", b )
            end
        when b == ?".ord || b == ?\\.ord then res << "\\" << b
        else res << b
        end
    end

    RubyVersions.when_19x( res << '"' ) { |s| s.force_encoding( "utf-8" ) }
end

.get_simple_escape(ch) ⇒ Object



532
533
534
535
536
537
538
539
# File 'lib/mingle.rb', line 532

def get_simple_escape( ch )
    
    if i = SIMPLE_ESCAPE_VALS.index( ch.chr )
        SIMPLE_ESCAPE_STRS[ 2 * i, 2 ]
    else
        nil
    end
end