-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprint.e
132 lines (121 loc) · 2.83 KB
/
sprint.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-----------------------
-- sPRINT.E v1.0 --
-- by Gabriel Boehme --
-- modified by --
-- matt Lewis --
-- updated 7/14/2008 --
-----------------------
-- 2008.07.14
-- * Updated to work with Euphoria v4.0
-- Modified print.e by GB to do the same thing, but to act as sprint()
-- include sequence.e
include std/text.e
type string(sequence s)
-- verify that we have a valid Euphoria-representable string
-- (ASCII chars 32-255, 9, 10, 13)
object x
for i = 1 to length(s) do
x = s[i]
if integer(x) then
if x < ' ' then
if not find(x, "\t\n\r") then
return 0
end if
elsif x > 255 then
return 0
end if
else
return 0
end if
end for
return 1
end type
integer fn
sequence result
constant ESCAPE_STRING = {"\\n", "\\t", "\\\"", "\\\\", "\\r"},
ESCAPED_CHARS = {'\n', '\t', '"', '\\', '\r'}
procedure SprintEuString(sequence s)
integer c, f
--puts(fn, '"')
result &= '"'
for i = 1 to length(s) do
c = s[i]
f = find(c, ESCAPED_CHARS)
if f then
--puts(fn, ESCAPE_STRING[f])
result &= ESCAPE_STRING[f]
else
--puts(fn, c)
result &= c
end if
end for
--puts(fn, '"')
result &= '"'
end procedure
procedure Sprint(sequence s)
integer len
object x
len = length(s)
if len and string(s) then
-- print the sequence out as a string
SprintEuString(s)
else
-- print the sequence out as...well, a sequence
--puts(fn, '{')
result &= '{'
for i = 1 to len do
x = s[i]
if atom(x) then
-- print out the numeric value
--print(fn, x)
result &= sprint(x)
else
Sprint(x)
end if
if i < len then
--puts(fn, ',')
result &= ','
end if
end for
--puts(fn, '}')
result &= '}'
end if
end procedure
without warning
-- pretty_sprint() is in std/pretty.e
global function pretty_sprint( object x)
result = ""
if atom(x) then
-- just an atom, printf the value
return sprint(x)
--printf(file, "%g", x)
else
-- a sequence!
--fn = file
if length(x) then
Sprint(x)
if result[1] = '"' then
result = result[2..length(result)-1]
end if
end if
return result
end if
end function
global function pretty_source( object x )
-- print out any Euphoria object, with strings in quotes
result = ""
if atom(x) then
-- just an atom, printf the value
return sprint(x)
--printf(file, "%g", x)
else
-- a sequence!
--fn = file
if length(x) then
Sprint(x)
else
result = "{}"
end if
return result
end if
end function