comparison src/luan/modules/Boot.luan @ 1716:b82767112d8e

add String.regex
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 24 Jul 2022 23:43:03 -0600
parents 46cf5137cb6b
children 2f3a8f16f583
comparison
equal deleted inserted replaced
1715:ad44e849c60c 1716:b82767112d8e
6 local ipairs = BasicLuan.ipairs 6 local ipairs = BasicLuan.ipairs
7 local set_metatable = BasicLuan.set_metatable 7 local set_metatable = BasicLuan.set_metatable
8 local load = BasicLuan.load 8 local load = BasicLuan.load
9 local type = BasicLuan.type 9 local type = BasicLuan.type
10 local StringLuan = require "java:luan.modules.StringLuan" 10 local StringLuan = require "java:luan.modules.StringLuan"
11 local match = StringLuan.match -- String.match 11 local contains = StringLuan.contains -- String.contains
12 local matches = StringLuan.matches -- String.matches 12 local RegexLuan = require "java:luan.modules.RegexLuan"
13 local IoLuan = require "java:luan.modules.IoLuan" 13 local IoLuan = require "java:luan.modules.IoLuan"
14 local LuanUrl = require "java:luan.modules.url.LuanUrl" 14 local LuanUrl = require "java:luan.modules.url.LuanUrl"
15 local LuanJava = require "java:luan.Luan" 15 local LuanJava = require "java:luan.Luan"
16 local LuanTable = require "java:luan.LuanTable" 16 local LuanTable = require "java:luan.LuanTable"
17 local get_local_cloned = BasicLuan.get_local_cloned 17 local get_local_cloned = BasicLuan.get_local_cloned
45 local local_metatable = { 45 local local_metatable = {
46 __index = get_local_cloned 46 __index = get_local_cloned
47 __new_index = set_local_cloned 47 __new_index = set_local_cloned
48 } 48 }
49 Boot.local_metatable = local_metatable 49 Boot.local_metatable = local_metatable
50
51
52 local function regex(pattern)
53 local regex = RegexLuan.new(pattern)
54 return {
55 java = regex
56 find = regex.find
57 gmatch = regex.gmatch
58 gsub = regex.gsub
59 match = regex.match
60 matches = regex.matches
61 set = regex.set
62 }
63 end
64 Boot.regex = regex
50 65
51 66
52 local function new_LuanIn(io) 67 local function new_LuanIn(io)
53 local this = {} 68 local this = {}
54 this.java = io 69 this.java = io
228 243
229 set_metatable(schemes,local_metatable) 244 set_metatable(schemes,local_metatable)
230 Boot.schemes = schemes 245 Boot.schemes = schemes
231 246
232 247
248 local uri_regex = regex("(?s)^([^:]+):(.*)$")
249
233 local function uri(name,options) 250 local function uri(name,options)
234 local scheme, location = match( name, "(?s)^([^:]+):(.*)$" ) 251 local scheme, location = uri_regex.match(name)
235 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" ) 252 scheme or error( "invalid Io.uri name '"..name.."', missing scheme" )
236 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" ) 253 local opener = schemes[scheme] or error( "invalid scheme '"..scheme.."' in '"..name.."'" )
237 return opener(location,options) 254 return opener(location,options)
238 end 255 end
239 Boot.uri = uri 256 Boot.uri = uri
247 return u.read_text() 264 return u.read_text()
248 end 265 end
249 266
250 function Boot.load_file(file) 267 function Boot.load_file(file)
251 if type(file) == "string" then 268 if type(file) == "string" then
252 if not matches(file,":") then 269 if not contains(file,":") then
253 file = "file:"..file 270 file = "file:"..file
254 end 271 end
255 local u = uri(file) 272 local u = uri(file)
256 if u==nil or not u.exists() then 273 if u==nil or not u.exists() then
257 return nil 274 return nil