Tue, 16 Jan 2018 17:25:50 +0000
README typofixes etc.
| 3 | 1 | -- |
| 2 | -- Copyright 2007-2008 Savarese Software Research Corporation. | |
| 3 | -- | |
| 4 | -- Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | -- you may not use this file except in compliance with the License. | |
| 6 | -- You may obtain a copy of the License at | |
| 7 | -- | |
| 8 | -- http://www.savarese.com/software/ApacheLicense-2.0 | |
| 9 | -- | |
| 10 | -- Unless required by applicable law or agreed to in writing, software | |
| 11 | -- distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | -- See the License for the specific language governing permissions and | |
| 14 | -- limitations under the License. | |
| 15 | -- | |
| 16 | ||
| 17 | local function slice(t, start, size) | |
| 18 | local sz = (size or (#t - start + 1)) | |
| 19 | local result = { } | |
| 20 | ||
| 21 | for i = 1, sz, 1 do | |
| 22 | result[i] = t[start + i - 1] | |
| 23 | end | |
| 24 | ||
| 25 | return result | |
| 26 | end | |
| 27 | ||
| 28 | local function dual_index(index1, index2) | |
| 29 | local index1_type = type(index1) | |
| 30 | ||
| 31 | if index1_type == "table" then | |
| 32 | return function(t,k) | |
| 33 | if index1[k] ~= nil then | |
| 34 | return index1[k] | |
| 35 | else | |
| 36 | return index2[k] | |
| 37 | end | |
| 38 | end | |
| 39 | elseif index1_type == "function" then | |
| 40 | return function(t,k) | |
| 41 | local v = index1(t,k) | |
| 42 | if v ~= nil then | |
| 43 | return v | |
| 44 | else | |
| 45 | return index2[k] | |
| 46 | end | |
| 47 | end | |
| 48 | end | |
| 49 | end | |
| 50 | ||
| 51 | local function merge_index(t1, t2) | |
| 52 | local mt = getmetatable(t1) | |
| 53 | ||
| 54 | if mt ~= nil then | |
| 55 | if mt.__index ~= nil then | |
| 56 | mt.__index = dual_index(mt.__index, t2) | |
| 57 | else | |
| 58 | mt.__index = t2 | |
| 59 | end | |
| 60 | return t1 | |
| 61 | else | |
| 62 | return setmetatable(t1, {__index = t2}) | |
| 63 | end | |
| 64 | end | |
| 65 | ||
| 66 | local function merge_table(t1, t2) | |
| 67 | local typet1 | |
| 68 | ||
| 69 | for k, v in pairs(t2) do | |
| 70 | typet1 = type(t1[k]) | |
| 71 | if t1[k] ~= v and type(v) == "table" then | |
| 72 | if typet1 == "table" then | |
| 73 | merge_table(t1[k], v) | |
| 74 | elseif typet1 == "nil" then | |
| 75 | t1[k] = v | |
| 76 | end | |
| 77 | end | |
| 78 | end | |
| 79 | ||
| 80 | return merge_index(t1, t2) | |
| 81 | end | |
| 82 | ||
| 83 | local function import(module_name, environment) | |
| 84 | return setfenv(package.loaders[2](module_name), environment)() | |
| 85 | end | |
| 86 | ||
| 87 | local function read_all(filename) | |
| 88 | local file = io.open(filename, "r") | |
| 89 | local data = ((file and file:read("*a")) or nil) | |
| 90 | if file then | |
| 91 | file:close() | |
| 92 | end | |
| 93 | return data | |
| 94 | end | |
| 95 | ||
| 96 | return { slice = slice, | |
| 97 | merge_index = merge_index, | |
| 98 | merge_table = merge_table, | |
| 99 | import = import, | |
| 100 | read_all = read_all } |