Class: String

Inherits:
Object show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#fast_xsObject

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'ext/fast_xs/fast_xs.c', line 156

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