Use new HTML escape function

This commit is contained in:
Takashi Kokubun 2016-08-13 13:16:14 +09:00
parent 6b9d651f31
commit 50bb33d39b
5 changed files with 27 additions and 33 deletions

View File

@ -1,18 +1,10 @@
require 'mkmf'
houdini_dir = File.expand_path('./houdini', __dir__)
$INCFLAGS << " -I#{houdini_dir}"
$CFLAGS << ' -Wall -Wextra'
$CFLAGS << ' -Wall -Wextra'
$srcs = %w[hamlit.c]
Dir[File.join(houdini_dir, '*.c')].each do |path|
src = File.basename(path)
if /mswin|mingw|cygwin|bccwin/ =~ RUBY_PLATFORM
FileUtils.cp(path, src)
else
FileUtils.ln_s(path, src, force: true)
end
$srcs << src
end
$srcs = %w[
hamlit.c
hescape.c
]
create_makefile('hamlit/hamlit')

View File

@ -1,6 +1,6 @@
#include <ruby.h>
#include <ruby/encoding.h>
#include "houdini.h"
#include "hescape.h"
#include "string.h"
VALUE mAttributeBuilder, mObjectRef;
@ -58,13 +58,14 @@ hyphenate(VALUE str)
static VALUE
escape_html(VALUE str)
{
gh_buf buf = GH_BUF_INIT;
char *buf;
unsigned int size;
Check_Type(str, T_STRING);
if (houdini_escape_html0(&buf, (const uint8_t *)RSTRING_PTR(str), RSTRING_LEN(str), 0)) {
str = rb_enc_str_new(buf.ptr, buf.size, rb_utf8_encoding());
gh_buf_free(&buf);
size = hesc_escape_html(&buf, RSTRING_PTR(str), RSTRING_LEN(str));
if (size > RSTRING_LEN(str)) {
str = rb_enc_str_new(buf, size, rb_utf8_encoding());
free((void *)buf);
}
return str;

View File

@ -1,8 +1,9 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hescape.h"
static const uint8_t *ESCAPED_STRING[] = {
static const char *ESCAPED_STRING[] = {
"",
"&quot;",
"&amp;",
@ -43,13 +44,14 @@ static const char HTML_ESCAPE_TABLE[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static uint8_t*
ensure_allocated(uint8_t *buf, size_t size, size_t *asize)
static char*
ensure_allocated(char *buf, size_t size, size_t *asize)
{
size_t new_size;
if (size < *asize)
return buf;
size_t new_size;
if (*asize == 0) {
new_size = size;
} else {
@ -68,20 +70,20 @@ ensure_allocated(uint8_t *buf, size_t size, size_t *asize)
}
size_t
hesc_escape_html(uint8_t **dest, const uint8_t *buf, size_t size)
hesc_escape_html(char **dest, const char *buf, size_t size)
{
size_t asize = 0, esc_i, esize = 0, i = 0, rbuf_end = 0;
const uint8_t *esc;
uint8_t *rbuf = NULL;
const char *esc;
char *rbuf = NULL;
while (i < size) {
// Loop here to skip non-escaped characters fast.
while (i < size && (esc_i = HTML_ESCAPE_TABLE[buf[i]]) == 0)
while (i < size && (esc_i = HTML_ESCAPE_TABLE[(int)buf[i]]) == 0)
i++;
if (esc_i) {
if (i < size && esc_i) {
esc = ESCAPED_STRING[esc_i];
rbuf = ensure_allocated(rbuf, sizeof(uint8_t) * (size + esize + ESC_LEN(esc_i) + 1), &asize);
rbuf = ensure_allocated(rbuf, sizeof(char) * (size + esize + ESC_LEN(esc_i) + 1), &asize);
// Copy pending characters and escaped string.
memmove(rbuf + rbuf_end, buf + (rbuf_end - esize), i - (rbuf_end - esize));
@ -94,7 +96,7 @@ hesc_escape_html(uint8_t **dest, const uint8_t *buf, size_t size)
if (rbuf_end == 0) {
// Return given buf and size if there are no escaped characters.
*dest = (uint8_t *)buf;
*dest = (char *)buf;
return size;
} else {
// Copy pending characters including NULL character.

View File

@ -2,7 +2,6 @@
#define HESCAPE_H
#include <sys/types.h>
#include <stdint.h>
/*
* Replace characters according to the following rules.
@ -16,6 +15,6 @@
*
* @return size of dest. If it's larger than len, dest is required to be freed.
*/
extern size_t hesc_escape_html(uint8_t **dest, const uint8_t *src, size_t size);
extern size_t hesc_escape_html(char **dest, const char *src, size_t size);
#endif

View File

@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/k0kubun/hamlit'
spec.license = 'MIT'
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|sample)/}) } + `git -C ext/hamlit/houdini ls-files -z`.split("\x0").map { |path| "ext/hamlit/houdini/#{path}" }
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|sample)/}) }
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.extensions = ['ext/hamlit/extconf.rb']