-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabs.red
197 lines (179 loc) · 5.84 KB
/
tabs.red
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
Red [
title: "DETAB & ENTAB mezzanines"
purpose: "Tabs to spaces conversion and back"
author: @hiiamboris
license: BSD-3
provides: [detab entab]
notes: {Supports multi-line text}
]
; #include %clock.red
; #include %assert.red
detab: entab: none
context [
spaces: insert/dup "" #"^(20)" 32 ;-- 32 spaces max are supported per tab
set 'detab function [
"Expand tabs in string STR"
str [string! binary!]
/into buf [string! binary!] "Speficy an out buffer (allocated otherwise)"
/size tab [integer!] "Specify Tab size (default: 8)"
][
buf: any [buf make str length? str]
tab: any [tab 8]
#assert [
tab > 0
tail? spaces
]
ok: parse/case s1: str [
collect into buf [
any [
keep any [#"^/" s1: | not #"^-" skip]
any [
s2: #"^-"
keep (skip spaces (offset? s1 s2) % tab - tab)
s1:
]
]
]
]
#assert [ok]
buf
]
nonspace: charset [not #"^(20)"]
set 'entab function [
"Convert leading spaces in STR into tabs"
str [string! binary!]
/into buf [string! binary!] "Speficy an out buffer (allocated otherwise)"
/size tab [integer!] "Specify Tab size (default: 8)"
][
buf: any [buf make str length? str]
tab: any [tab 8] tab-1: tab - 1
#assert [
tab > 0
tail? spaces
]
ok: parse/case str [
collect into buf [
any [
s1: #" " [
s2: tab-1 [#" " s2:] keep (#"^-")
| :s1 keep :s2
]
| keep #"^-"
| keep thru [#"^/" | end]
]
]
]
#assert [ok]
buf
]
]
comment [
; slower versions:
detab2: func [s [string!] /into buf [string!] /size tab /local s1 s2] [
spaces: tail " " ;-- 32 spaces
append buf: any [buf copy ""] s
parse/case buf [any [
p: change #"^-" (
i: index? p
skip spaces -1 + i - round/to/ceiling i tab
)
| skip
]]
buf
]
detab1: func [s [string!] /into buf [string!] /size tab /local s1 s2] [
spaces: tail " " ;-- 32 spaces
buf: any [buf copy ""]
parse/case s [
any [
s1: to #"^-" s2: skip
(append/part buf s1 s2
append/dup buf #"^(20)" (tab - ((length? buf) % tab)) )
]
(append buf s1)
]
buf
]
detab3: func [s [string!] /into buf [string!] /size tab /local s2] [
buf: any [buf copy ""]
while [not tail? s] [
either s2: find/case s #"^-" [
append/part buf s s2
append/dup buf #"^(20)" (tab - ((length? buf) % tab))
][ append buf s break ]
s: next s2
]
buf
]
]
; tests
; s: "^-1 2 3 4^-1^-2 3 4 5^-|abcdefgh^-abcdefgh"
; buf: ""
; recycle/off
; probe "|-------|-------|-------|-------|-------|-------|-------|-------|"
; probe detab s
; probe entab detab s
; ; probe detab1 s clear buf
; ; probe detab2 s clear buf
; clock/times [detab/into s clear buf] 10000
; s: detab s
; clock/times [entab/into s clear buf] 10000
; clock/times [entab1/into s clear buf] 10000
; clock/times [entab2/into s clear buf] 10000
; clock/times [entab3/into s clear buf] 10000
#assert [
"" = detab ""
" " = detab "^-"
" " = detab "^-^-"
"1 " = detab "1^-^-"
"1 1 " = detab "1^-1^-"
" 1 " = detab "^-1^-"
"1234567 " = detab "1234567^-^-"
"12345678 " = detab "12345678^-"
"123456789 " = detab "123456789^-"
"123456789012345 " = detab "123456789012345^-"
" 1234567 " = detab "^-1234567^-"
"1 ^/ 2 ^/ ^/" = detab/size "1^-^/^-2^-^/^-^/" 4
"" = entab ""
"1" = entab "1"
"^-" = entab " "
"^-^-" = entab " "
" 1" = entab " 1"
" 1 " = entab " 1 "
"^-1 " = entab " 1 "
"1 " = entab "1 "
"^- 1" = entab " 1"
"^- 12" = entab " 12"
"^- 1234567" = entab " 1234567"
"^-12345678" = entab " 12345678"
" 123456789" = entab " 123456789"
"1234567890123456" = entab "1234567890123456"
"^-^- 1^/^-2" = entab/size "^- 1^/ 2" 4 ;-- allows mixing tabs and spaces; resets at newlines
"" = to "" detab to #{} ""
" " = to "" detab to #{} "^-"
" " = to "" detab to #{} "^-^-"
"1 " = to "" detab to #{} "1^-^-"
"1 1 " = to "" detab to #{} "1^-1^-"
" 1 " = to "" detab to #{} "^-1^-"
"1234567 " = to "" detab to #{} "1234567^-^-"
"12345678 " = to "" detab to #{} "12345678^-"
"123456789 " = to "" detab to #{} "123456789^-"
"123456789012345 " = to "" detab to #{} "123456789012345^-"
" 1234567 " = to "" detab to #{} "^-1234567^-"
"1 ^/ 2 ^/ ^/" = to "" detab/size to #{} "1^-^/^-2^-^/^-^/" 4
"" = to "" entab to #{} ""
"1" = to "" entab to #{} "1"
"^-" = to "" entab to #{} " "
"^-^-" = to "" entab to #{} " "
" 1" = to "" entab to #{} " 1"
" 1 " = to "" entab to #{} " 1 "
"^-1 " = to "" entab to #{} " 1 "
"1 " = to "" entab to #{} "1 "
"^- 1" = to "" entab to #{} " 1"
"^- 12" = to "" entab to #{} " 12"
"^- 1234567" = to "" entab to #{} " 1234567"
"^-12345678" = to "" entab to #{} " 12345678"
" 123456789" = to "" entab to #{} " 123456789"
"1234567890123456" = to "" entab to #{} "1234567890123456"
"^-^- 1^/^-2" = to "" entab/size to #{} "^- 1^/ 2" 4 ;-- allows mixing tabs and spaces; resets at newlines
]