2
Copyright (c) 2007, Niels Martin Hansen, Rodrigo Braz Monteiro
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
8
* Redistributions of source code must retain the above copyright notice,
9
this list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation
12
and/or other materials provided with the distribution.
13
* Neither the name of the Aegisub Group nor the names of its contributors
14
may be used to endorse or promote products derived from this software
15
without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
POSSIBILITY OF SUCH DAMAGE.
30
-- Unicode (UTF-8) support functions for Aegisub Automation 4 Lua
31
-- http://www.ietf.org/rfc/rfc2279.txt
35
-- Return the number of bytes occupied by the character starting at the i'th byte in s
36
function charwidth(s, i)
37
local b = s:byte(i or 1)
39
--aegisub.debug.out(3, "unicode.charwidth of '%s' @ %d, nil byte\n", s, i)
40
-- FIXME, something in karaskel results in this case, shouldn't happen
41
-- What would "proper" behaviour be? Zero? Or just explode?
56
-- Actually there are more possibilities, but those aren't really legal
59
-- Returns an iterator function for iterating over the characters in s
61
local curchar, i = 0, 1
68
local width = charwidth(s, i)
72
return s:sub(j, i-1), curchar
78
-- Returns the number of characters in s
79
-- Runs in O(s:len()) time!
88
-- Get codepoint of first char in s
92
if s:byte(1) < 128 then
96
-- Use a naive decoding algorithm, and assume input is valid
100
-- prefix byte is 110xxxxx
104
-- prefix byte is 11100000
108
-- prefix byte is 11110000
112
-- prefix byte is 11111000
116
-- prefix byte is 11111100
122
res = res*64 + s:byte(i) - 128