Mercurial Hosting > luan
comparison core/src/luan/modules/Io.luan @ 539:473e456444ff
Remove object-oriented primitive methods for string and binary
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 01 Jun 2015 17:53:55 -0600 |
parents | 92c3d22745b8 |
children | 18504c41b0be |
comparison
equal
deleted
inserted
replaced
538:919b9410008e | 539:473e456444ff |
---|---|
17 local to_string = Luan.to_string | 17 local to_string = Luan.to_string |
18 local type = Luan.type | 18 local type = Luan.type |
19 local try = Luan.try | 19 local try = Luan.try |
20 local ipairs = Luan.ipairs | 20 local ipairs = Luan.ipairs |
21 local pairs = Luan.pairs | 21 local pairs = Luan.pairs |
22 local values = Luan.values | |
22 local Table = require "luan:Table" | 23 local Table = require "luan:Table" |
24 local unpack = Table.unpack | |
25 local String = require "luan:String" | |
26 local encode = String.encode | |
27 local matches = String.matches | |
28 | |
23 | 29 |
24 function M.print_to(out,...) | 30 function M.print_to(out,...) |
25 local list = {} | 31 local list = {} |
26 for v in Luan.values(...) do | 32 for v in values(...) do |
27 list[#list+1] = to_string(v) | 33 list[#list+1] = to_string(v) |
28 list[#list+1] = '\t' | 34 list[#list+1] = '\t' |
29 end | 35 end |
30 if #list == 0 then | 36 if #list == 0 then |
31 out.write( '\n' ) | 37 out.write( '\n' ) |
32 else | 38 else |
33 list[#list] = '\n' | 39 list[#list] = '\n' |
34 out.write( Table.unpack(list) ) | 40 out.write( unpack(list) ) |
35 end | 41 end |
36 end | 42 end |
37 | 43 |
38 function M.print(...) | 44 function M.print(...) |
39 M.print_to(M.stdout,...) | 45 M.print_to(M.stdout,...) |
59 in_list[key] = true | 65 in_list[key] = true |
60 end | 66 end |
61 for key, value in pairs(obj) do | 67 for key, value in pairs(obj) do |
62 if in_list[key] ~= true then | 68 if in_list[key] ~= true then |
63 if is_first then is_first = false else %>, <% end | 69 if is_first then is_first = false else %>, <% end |
64 if type(key) == "string" and key.match "^[a-zA-Z_][a-zA-Z_0-9]*$" ~= nil then | 70 if type(key) == "string" and matches(key,"[a-zA-Z_][a-zA-Z_0-9]*") ~= nil then |
65 %><%=key%><% | 71 %><%=key%><% |
66 elseif type(key) == "table" then | 72 elseif type(key) == "table" then |
67 %>[<<%=key%>>]<% | 73 %>[<<%=key%>>]<% |
68 else | 74 else |
69 %>[<%do_repr(key,done)%>]<% | 75 %>[<%do_repr(key,done)%>]<% |
71 %>=<% do_repr(value,done) | 77 %>=<% do_repr(value,done) |
72 end | 78 end |
73 end | 79 end |
74 %>}<% | 80 %>}<% |
75 elseif tp == "string" then | 81 elseif tp == "string" then |
76 %>"<%=obj.encode()%>"<% | 82 %>"<%=encode(obj)%>"<% |
77 elseif tp == "nil" or tp == "boolean" or tp == "number" then | 83 elseif tp == "nil" or tp == "boolean" or tp == "number" then |
78 %><%=obj%><% | 84 %><%=obj%><% |
79 else | 85 else |
80 %><<%=obj%>><% | 86 %><<%=obj%>><% |
81 end | 87 end |
105 local NO = {} | 111 local NO = {} |
106 M.NO = NO | 112 M.NO = NO |
107 | 113 |
108 function M.dont_write_when_no(write_fn) | 114 function M.dont_write_when_no(write_fn) |
109 return function(...) | 115 return function(...) |
110 for v in Luan.values(...) do | 116 for v in values(...) do |
111 if v == NO then | 117 if v == NO then |
112 return | 118 return |
113 end | 119 end |
114 end | 120 end |
115 write_fn(...) | 121 write_fn(...) |