Improved performance

This commit is contained in:
Łukasz Magiera 2016-01-18 19:42:32 +01:00
parent 75f386b03a
commit 2259a83d76
6 changed files with 2209 additions and 2174 deletions

3
.gitattributes vendored Normal file
View File

@ -0,0 +1,3 @@
*.lua -crlf
*.h -crlf
*.c -crlf

View File

@ -1,5 +1,9 @@
# TARGET=arm-none-eabi
#CC=$(TARGET)-gcc
CC=gcc CC=gcc
CFLAGS=-g -std=gnu99 -Isrc/lib/lua -Iinclude
CFLAGS=-O2 -std=gnu99 -Isrc/lib/lua -Iinclude
BUILD = bin/ BUILD = bin/
SOURCE = src/c/ SOURCE = src/c/
@ -22,14 +26,9 @@ OBJECTS := $(patsubst $(SOURCE)%.c, $(BUILD)%.c.o, $(CFILES))
$(BUILDDIRECTORIES): $(BUILDDIRECTORIES):
mkdir -p $@ mkdir -p $@
#Clean
#Build #Build
all: smallclean $(BUILDDIRECTORIES) luaresources $(BUILD)lupi all: smallclean $(BUILDDIRECTORIES) luaresources $(BUILD)lupi
smallclean:
find . -name '*~' -type f -exec rm {} \;
build: clean all build: clean all
$(BUILD)lupi: $(OBJECTS) $(BUILD)lupi: $(OBJECTS)
@ -51,3 +50,10 @@ cleanresourcues:
clean: cleanresourcues clean: cleanresourcues
-rm -rf $(BUILD) -rm -rf $(BUILD)
smallclean:
find . -name '*~' -type f -exec rm {} \;
# Other
.PHONY: clean cleanresourcues luaresources build smallclean all

View File

@ -157,4 +157,8 @@ function api.totalMemory()
return native.totalMemory() return native.totalMemory()
end end
function api.shutdown()
os.exit(0)
end
return computer return computer

View File

@ -17,6 +17,7 @@ io.write = function(...)
io.flush() io.flush()
native.sleep(20000) native.sleep(20000)
end]]-- end]]--
local write = io.write local write = io.write
local flush = io.flush local flush = io.flush

View File

@ -1,307 +1,328 @@
-- $Id: utf8.lua 179 2009-04-03 18:10:03Z pasta $ -- $Id: utf8.lua 179 2009-04-03 18:10:03Z pasta $
-- --
-- Provides UTF-8 aware string functions implemented in pure lua: -- Provides UTF-8 aware string functions implemented in pure lua:
-- * string.utf8len(s) -- * string.utf8len(s)
-- * string.utf8sub(s, i, j) -- * string.utf8sub(s, i, j)
-- * string.utf8reverse(s) -- * string.utf8reverse(s)
-- --
-- If utf8data.lua (containing the lower<->upper case mappings) is loaded, these -- If utf8data.lua (containing the lower<->upper case mappings) is loaded, these
-- additional functions are available: -- additional functions are available:
-- * string.utf8upper(s) -- * string.utf8upper(s)
-- * string.utf8lower(s) -- * string.utf8lower(s)
-- --
-- All functions behave as their non UTF-8 aware counterparts with the exception -- All functions behave as their non UTF-8 aware counterparts with the exception
-- that UTF-8 characters are used instead of bytes for all units. -- that UTF-8 characters are used instead of bytes for all units.
--[[ --[[
Copyright (c) 2006-2007, Kyle Smith Copyright (c) 2006-2007, Kyle Smith
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, * Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright * Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be * Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without used to endorse or promote products derived from this software without
specific prior written permission. specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]] --]]
-- ABNF from RFC 3629 -- ABNF from RFC 3629
-- --
-- UTF8-octets = *( UTF8-char ) -- UTF8-octets = *( UTF8-char )
-- UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4 -- UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
-- UTF8-1 = %x00-7F -- UTF8-1 = %x00-7F
-- UTF8-2 = %xC2-DF UTF8-tail -- UTF8-2 = %xC2-DF UTF8-tail
-- UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) / -- UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
-- %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail ) -- %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
-- UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) / -- UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
-- %xF4 %x80-8F 2( UTF8-tail ) -- %xF4 %x80-8F 2( UTF8-tail )
-- UTF8-tail = %x80-BF -- UTF8-tail = %x80-BF
-- --
local strbyte, strlen, strsub, type = string.byte, string.len, string.sub, type local strbyte, strlen, strsub, type = string.byte, string.len, string.sub, type
local utf8 = {} local lutf8 = utf8
local utf8 = {}
-- returns the number of bytes used by the UTF-8 character at byte i in s
-- also doubles as a UTF-8 character validator -- returns the number of bytes used by the UTF-8 character at byte i in s
local function utf8charbytes(s, i) -- also doubles as a UTF-8 character validator
-- argument defaults local function utf8charbytes(s, i)
i = i or 1 -- argument defaults
i = i or 1
-- argument checking
if type(s) ~= "string" then -- argument checking
error("bad argument #1 to 'utf8charbytes' (string expected, got ".. type(s).. ")") if type(s) ~= "string" then
end error("bad argument #1 to 'utf8charbytes' (string expected, got ".. type(s).. ")")
if type(i) ~= "number" then end
error("bad argument #2 to 'utf8charbytes' (number expected, got ".. type(i).. ")") if type(i) ~= "number" then
end error("bad argument #2 to 'utf8charbytes' (number expected, got ".. type(i).. ")")
end
local c = strbyte(s, i)
local c = strbyte(s, i)
-- determine bytes needed for character, based on RFC 3629
-- validate byte 1 -- determine bytes needed for character, based on RFC 3629
if c > 0 and c <= 127 then -- validate byte 1
-- UTF8-1 if c > 0 and c <= 127 then
return 1 -- UTF8-1
return 1
elseif c >= 194 and c <= 223 then
-- UTF8-2 elseif c >= 194 and c <= 223 then
local c2 = strbyte(s, i + 1) -- UTF8-2
local c2 = strbyte(s, i + 1)
if not c2 then
error("UTF-8 string terminated early") if not c2 then
end error("UTF-8 string terminated early")
end
-- validate byte 2
if c2 < 128 or c2 > 191 then -- validate byte 2
error("Invalid UTF-8 character") if c2 < 128 or c2 > 191 then
end error("Invalid UTF-8 character")
end
return 2
return 2
elseif c >= 224 and c <= 239 then
-- UTF8-3 elseif c >= 224 and c <= 239 then
local c2 = strbyte(s, i + 1) -- UTF8-3
local c3 = strbyte(s, i + 2) local c2 = strbyte(s, i + 1)
local c3 = strbyte(s, i + 2)
if not c2 or not c3 then
error("UTF-8 string terminated early") if not c2 or not c3 then
end error("UTF-8 string terminated early")
end
-- validate byte 2
if c == 224 and (c2 < 160 or c2 > 191) then -- validate byte 2
error("Invalid UTF-8 character") if c == 224 and (c2 < 160 or c2 > 191) then
elseif c == 237 and (c2 < 128 or c2 > 159) then error("Invalid UTF-8 character")
error("Invalid UTF-8 character") elseif c == 237 and (c2 < 128 or c2 > 159) then
elseif c2 < 128 or c2 > 191 then error("Invalid UTF-8 character")
error("Invalid UTF-8 character") elseif c2 < 128 or c2 > 191 then
end error("Invalid UTF-8 character")
end
-- validate byte 3
if c3 < 128 or c3 > 191 then -- validate byte 3
error("Invalid UTF-8 character") if c3 < 128 or c3 > 191 then
end error("Invalid UTF-8 character")
end
return 3
return 3
elseif c >= 240 and c <= 244 then
-- UTF8-4 elseif c >= 240 and c <= 244 then
local c2 = strbyte(s, i + 1) -- UTF8-4
local c3 = strbyte(s, i + 2) local c2 = strbyte(s, i + 1)
local c4 = strbyte(s, i + 3) local c3 = strbyte(s, i + 2)
local c4 = strbyte(s, i + 3)
if not c2 or not c3 or not c4 then
error("UTF-8 string terminated early") if not c2 or not c3 or not c4 then
end error("UTF-8 string terminated early")
end
-- validate byte 2
if c == 240 and (c2 < 144 or c2 > 191) then -- validate byte 2
error("Invalid UTF-8 character") if c == 240 and (c2 < 144 or c2 > 191) then
elseif c == 244 and (c2 < 128 or c2 > 143) then error("Invalid UTF-8 character")
error("Invalid UTF-8 character") elseif c == 244 and (c2 < 128 or c2 > 143) then
elseif c2 < 128 or c2 > 191 then error("Invalid UTF-8 character")
error("Invalid UTF-8 character") elseif c2 < 128 or c2 > 191 then
end error("Invalid UTF-8 character")
end
-- validate byte 3
if c3 < 128 or c3 > 191 then -- validate byte 3
error("Invalid UTF-8 character") if c3 < 128 or c3 > 191 then
end error("Invalid UTF-8 character")
end
-- validate byte 4
if c4 < 128 or c4 > 191 then -- validate byte 4
error("Invalid UTF-8 character") if c4 < 128 or c4 > 191 then
end error("Invalid UTF-8 character")
end
return 4
return 4
else
error("Invalid UTF-8 character") else
end error("Invalid UTF-8 character")
end end
end
utf8.charbytes = utf8charbytes
utf8.charbytes = utf8charbytes
-- returns the number of characters in a UTF-8 string
local function utf8len(s) -- returns the number of characters in a UTF-8 string
-- argument checking local function utf8len(s)
if type(s) ~= "string" then -- argument checking
error("bad argument #1 to 'utf8len' (string expected, got ".. type(s).. ")") if type(s) ~= "string" then
end error("bad argument #1 to 'utf8len' (string expected, got ".. type(s).. ")")
end
local pos = 1
local bytes = strlen(s) local pos = 1
local len = 0 local bytes = strlen(s)
local len = 0
while pos <= bytes do
len = len + 1 while pos <= bytes do
pos = pos + utf8charbytes(s, pos) len = len + 1
end pos = pos + utf8charbytes(s, pos)
end
return len
end return len
end
utf8.len = utf8len
utf8.len = utf8len
-- functions identically to string.sub except that i and j are UTF-8 characters
-- instead of bytes -- functions identically to string.sub except that i and j are UTF-8 characters
local function utf8sub(s, i, j) -- instead of bytes
-- argument defaults local function utf8sub(s, i, j)
j = j or -1 -- argument defaults
j = j or -1
-- argument checking
if type(s) ~= "string" then -- argument checking
error("bad argument #1 to 'utf8sub' (string expected, got ".. type(s).. ")") if type(s) ~= "string" then
end error("bad argument #1 to 'utf8sub' (string expected, got ".. type(s).. ")")
if type(i) ~= "number" then end
error("bad argument #2 to 'utf8sub' (number expected, got ".. type(i).. ")") if type(i) ~= "number" then
end error("bad argument #2 to 'utf8sub' (number expected, got ".. type(i).. ")")
if type(j) ~= "number" then end
error("bad argument #3 to 'utf8sub' (number expected, got ".. type(j).. ")") if type(j) ~= "number" then
end error("bad argument #3 to 'utf8sub' (number expected, got ".. type(j).. ")")
end
local pos = 1
local bytes = strlen(s) local pos = 1
local len = 0 local bytes = strlen(s)
local len = 0
-- only set l if i or j is negative
local l = (i >= 0 and j >= 0) or utf8len(s) -- only set l if i or j is negative
local startChar = (i >= 0) and i or l + i + 1 local l = (i >= 0 and j >= 0) or utf8len(s)
local endChar = (j >= 0) and j or l + j + 1 local startChar = (i >= 0) and i or l + i + 1
local endChar = (j >= 0) and j or l + j + 1
-- can't have start before end!
if startChar > endChar then -- can't have start before end!
return "" if startChar > endChar then
end return ""
end
-- byte offsets to pass to string.sub
local startByte, endByte = 1, bytes -- byte offsets to pass to string.sub
local startByte, endByte = 1, bytes
while pos <= bytes do
len = len + 1 while pos <= bytes do
len = len + 1
if len == startChar then
startByte = pos if len == startChar then
end startByte = pos
end
pos = pos + utf8charbytes(s, pos)
pos = pos + utf8charbytes(s, pos)
if len == endChar then
endByte = pos - 1 if len == endChar then
break endByte = pos - 1
end break
end end
end
return strsub(s, startByte, endByte)
end return strsub(s, startByte, endByte)
end
utf8.sub = utf8sub
function utf8.sub(s,i,j)
-- replace UTF-8 characters based on a mapping table i = i or 1
local function utf8replace(s, mapping) j = j or math.maxinteger
-- argument checking if i<1 or j<1 then
if type(s) ~= "string" then local n = lutf8.len(s)
error("bad argument #1 to 'utf8replace' (string expected, got ".. type(s).. ")") if not n then return nil end
end if i<0 then i = n+1+i end
if type(mapping) ~= "table" then if j<0 then j = n+1+j end
error("bad argument #2 to 'utf8replace' (table expected, got ".. type(mapping).. ")") if i<0 then i = 1 elseif i>n then i = n end
end if j<0 then j = 1 elseif j>n then j = n end
end
local pos = 1 if j<i then return "" end
local bytes = strlen(s) i = lutf8.offset(s,i) or math.maxinteger
local charbytes j = lutf8.offset(s,j+1) or math.maxinteger
local newstr = "" if i and j then return s:sub(i,j-1)
elseif i then return s:sub(i)
while pos <= bytes do else return ""
charbytes = utf8charbytes(s, pos) end
local c = strsub(s, pos, pos + charbytes - 1) end
newstr = newstr .. (mapping[c] or c) --utf8.sub = utf8sub
pos = pos + charbytes -- replace UTF-8 characters based on a mapping table
end local function utf8replace(s, mapping)
-- argument checking
return newstr if type(s) ~= "string" then
end error("bad argument #1 to 'utf8replace' (string expected, got ".. type(s).. ")")
end
-- identical to string.upper except it knows about unicode simple case conversions if type(mapping) ~= "table" then
local function utf8upper(s) error("bad argument #2 to 'utf8replace' (table expected, got ".. type(mapping).. ")")
return utf8replace(s, modules.utf8data.lc_uc) end
end
local pos = 1
utf8.upper = utf8upper local bytes = strlen(s)
local charbytes
-- identical to string.lower except it knows about unicode simple case conversions local newstr = ""
local function utf8lower(s)
return utf8replace(s, modules.utf8data.uc_lc) while pos <= bytes do
end charbytes = utf8charbytes(s, pos)
local c = strsub(s, pos, pos + charbytes - 1)
utf8.lower = utf8lower
newstr = newstr .. (mapping[c] or c)
-- identical to string.reverse except that it supports UTF-8
local function utf8reverse(s) pos = pos + charbytes
-- argument checking end
if type(s) ~= "string" then
error("bad argument #1 to 'utf8reverse' (string expected, got ".. type(s).. ")") return newstr
end end
local bytes = strlen(s) -- identical to string.upper except it knows about unicode simple case conversions
local pos = bytes local function utf8upper(s)
local charbytes return utf8replace(s, modules.utf8data.lc_uc)
local newstr = "" end
while pos > 0 do utf8.upper = utf8upper
c = strbyte(s, pos)
while c >= 128 and c <= 191 do -- identical to string.lower except it knows about unicode simple case conversions
pos = pos - 1 local function utf8lower(s)
c = strbyte(pos) return utf8replace(s, modules.utf8data.uc_lc)
end end
charbytes = utf8charbytes(s, pos) utf8.lower = utf8lower
newstr = newstr .. strsub(s, pos, pos + charbytes - 1) -- identical to string.reverse except that it supports UTF-8
local function utf8reverse(s)
pos = pos - 1 -- argument checking
end if type(s) ~= "string" then
error("bad argument #1 to 'utf8reverse' (string expected, got ".. type(s).. ")")
return newstr end
end
local bytes = strlen(s)
utf8.reverse = utf8reverse local pos = bytes
local charbytes
return utf8 local newstr = ""
while pos > 0 do
c = strbyte(s, pos)
while c >= 128 and c <= 191 do
pos = pos - 1
c = strbyte(pos)
end
charbytes = utf8charbytes(s, pos)
newstr = newstr .. strsub(s, pos, pos + charbytes - 1)
pos = pos - 1
end
return newstr
end
utf8.reverse = utf8reverse
return utf8

File diff suppressed because it is too large Load Diff