class String - The Hpricot Reference

# class String

### Public Instance Methods

fast_xs() click to toggle source

escapes strings for XML The double-quote (“) character is translated to ”&quot;“

    static VALUE fast_xs(VALUE self)
    {
            long i;
            VALUE array;
            char *c;
            size_t s_len;
            VALUE *tmp;
            VALUE rv;

            array = rb_rescue(unpack_utf8, self, unpack_uchar, self);

            for (tmp = RARRAY_PTR(array), s_len = i = RARRAY_LEN(array);
                 --i >= 0;
                 tmp++) {
                    int n = NUM2INT(*tmp);
                    if (likely(n < 128)) {
                            if (unlikely(n == '"'))
                                    s_len += (sizeof("&quot;") - 2);
                            if (unlikely(n == '&'))
                                    s_len += (sizeof("&amp;") - 2);
                            if (unlikely(n == '>' || n == '<'))
                                    s_len += (sizeof("&gt;") - 2);
                            continue;
                    }

                    CP_1252_ESCAPE(n);

                    if (VALID_VALUE(n))
                            s_len += bytes_for(n) - 1;
            }

            rv = rb_str_new(NULL, s_len);
            ASSOCIATE_INDEX(rv, rb_default_external_encoding());
            c = RSTRING_PTR(rv);

            for (tmp = RARRAY_PTR(array), i = RARRAY_LEN(array); --i >= 0; tmp++)
                    c += escape(c, NUM2INT(*tmp));

            return rv;
    }