File: utf8_processor.lua

package info (click to toggle)
lua-json 1.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: makefile: 71; php: 3
file content (56 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local lpeg = require("lpeg")

local string = string

local floor = require("math").floor

local _ENV = nil

local function encode_utf(codepoint)
	if codepoint > 0x10FFFF then
		error("Codepoint > 10FFFF cannot be encoded")
	elseif codepoint > 0xFFFF then
		-- Surrogate pair needed
		codepoint = codepoint - 0x10000
		local first, second = floor(codepoint / 0x0400) + 0xD800, codepoint % 0x0400 + 0xDC00
		return ("\\u%.4X\\u%.4X"):format(first, second)
	else
		return ("\\u%.4X"):format(codepoint)
	end
end

-- decode a two-byte UTF-8 sequence
local function f2 (s)
	local c1, c2 = string.byte(s, 1, 2)
	return encode_utf(c1 * 64 + c2 - 12416)
end

-- decode a three-byte UTF-8 sequence
local function f3 (s)
	local c1, c2, c3 = string.byte(s, 1, 3)
	return encode_utf((c1 * 64 + c2) * 64 + c3 - 925824)
end

-- decode a four-byte UTF-8 sequence
local function f4 (s)
	local c1, c2, c3, c4 = string.byte(s, 1, 4)
	return encode_utf(((c1 * 64 + c2) * 64 + c3) * 64 + c4 - 63447168)
end

local cont = lpeg.R("\128\191")   -- continuation byte

local utf8 = lpeg.R("\0\127") -- Do nothing here
	+ lpeg.R("\194\223") * cont / f2
	+ lpeg.R("\224\239") * cont * cont / f3
	+ lpeg.R("\240\244") * cont * cont * cont / f4

local utf8_decode_pattern = lpeg.Cs(utf8^0) * -1


local function process(s)
	return utf8_decode_pattern:match(s)
end

return {
    process = process
}