Last active
November 9, 2018 08:04
-
-
Save greggirwin/c56da21b5d2b5536424acff1fdc258a5 to your computer and use it in GitHub Desktop.
Red ENTAB/DETAB mezzanines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [] | |
; Steeve gets credit for this one | |
detab: function [ | |
"Converts leading tabs in a string to spaces. (tab size 4)" | |
string [any-string!] "(modified)" | |
/size | |
sz [integer!] "Number of spaces per tab" | |
/all "Change all, not just leading" | |
][ | |
sz: max 1 any [sz 4] ; size must be at least 1 | |
buf: append/dup clear " " space sz | |
parse string [ | |
BOL: some [ | |
; If we see a tab, mark its position, then change it to the...ready? | |
; offset into our buffer of spaces, based on the the difference | |
; between our current position and the beginning of the line, modulo | |
; the tab size. | |
pos: change tab (skip buf (offset? BOL pos) % sz) | |
| lf BOL: ; Set our beginning-of-line marker | |
| if (all) to [tab | lf] ; Skip over non-tab chars | |
| space ; Spaces just move us forward in the line | |
| thru lf BOL: ; Skip to end of line and set our marker | |
] | |
] | |
string | |
] | |
detab-tests: [ | |
"" "" | |
"^-" " " | |
" ^-" " " | |
" ^-" " " | |
" ^-" " " | |
" ^-" " " | |
" ^-" " " | |
" ^- ^-" " " | |
" ^- ^-" " " | |
" ^- ^-^-" " " | |
" ^- ^-^-^-" " " | |
"^-abc^-" " abc^-" | |
"^-abc^-^/" " abc^-^/" | |
"^-abc^-^/^-def^-" " abc^-^/ def^-" | |
" ^- ^-^-" " " | |
"^-abc ^-" " abc ^-" ; 10 | |
" ^- ^-^-^-" " " ; 16 | |
" ^- ^-^-" " " ; 16 | |
] | |
foreach [inp out] detab-tests [ | |
if out <> res: detab inp [ | |
print ['FAIL! 'expected mold out "but got" mold res] | |
] | |
] | |
detab-tests: [ | |
"^-abc^-" " abc " ; 8 | |
"^-abc ^-" " abc " ; 12 | |
"^-abc^-^/^-def^-" " abc ^/ def " ; 17 | |
] | |
foreach [inp out] detab-tests [ | |
if out <> res: detab/all inp [ | |
print ['FAIL! 'expected mold out "but got" mold res] | |
] | |
] | |
detab-tests: [ | |
"^-" 2 " " | |
" ^-" 2 " " | |
" ^-" 2 " " | |
" ^- ^-^-^-" 2 " " | |
" ^- ^- ^-^-" 2 " " | |
] | |
foreach [inp sz out] detab-tests [ | |
if out <> res: detab/size/all inp sz [ | |
print ['FAIL! 'expected mold out "but got" mold res] | |
] | |
] | |
;------------------------------------------------------------------------------- | |
entab: function [ | |
"Converts leading spaces in a string to tabs. (tab size 4)" | |
string [any-string!] "(modified)" | |
/size | |
sz [integer!] "Number of spaces per tab (must be at least 1)" | |
/all "Change all, not just leading" | |
][ | |
sz: max 1 any [sz 4] ; size must be at least 1 | |
sz-1: max 1 sz - 1 ; can't have 0 as a m..n size for parse | |
parse string [ | |
some [ | |
any [change sz space tab | change [1 sz-1 space tab] tab] | |
if (not all) [thru [newline | end]] | |
| skip | |
] | |
] | |
string | |
] | |
entab-tests: [ | |
"" "" | |
" " "^-" | |
" ^-" "^-" | |
" ^-" "^-" | |
" ^-" "^-" | |
" ^-" "^-^-" | |
"a b c d e f g" "a b c d e f g" | |
" a b c d e f g" "^-a b c d e f g" | |
"a b c d e f g " "a b c d e f g " | |
" a b c d e f g " " a b c d e f g " | |
" a b c d e f g " "^-^-a b c d e f g " | |
"a b c^/ d e f g" "a b c^/^-d e f g" | |
"a b c^/ d e f g" "a b c^/^-d e f g" | |
" a b c^/ d e f g^/ " "^-a b c^/^-d e f g^/^-" | |
] | |
foreach [inp out] entab-tests [ | |
if out <> res: entab inp [ | |
print ['FAIL! 'expected mold out "but got" mold res] | |
] | |
] | |
entab-tests: [ | |
{ a ^/ b ^/ c d e f g^/ } 2 {^-^-^-^-a ^/ b ^/ c d e f g^/^-^-} | |
{ a ^/ b ^/ c d e f g^/ } 2 {^-^-a ^/ b ^/ c d e f g^/^-} | |
] | |
foreach [inp sz out] entab-tests [ | |
if out <> res: entab/size inp sz [ | |
print ['FAIL! 'expected mold out "but got" mold res] | |
] | |
] | |
entab-tests: [ | |
"a b c d e f g" 4 "a b c^-d e f g" | |
" a b c d e f g" 4 "^-a b c^-^-d e f g" | |
"a b c d e f g " 4 "a b c^-^-d e f g^-" | |
" a b c d e f g " 4 " a b c^- d e f g " | |
" a b c d e f g " 4 "^-^-a b c^-^-d e f g^-^-" | |
{ a} 1 {^-a} | |
{ a ^/ b ^/ c d e f g^/ } 2 {^-^-^-^-a^-^-^/ b^-^-^/ c^-^-^-^-d e f g^/^-^-} | |
{ a ^/ b ^/ c d e f g^/ } 2 {^-^-a^-^/ b^-^/ c^-^-d e f g^/^-} | |
] | |
foreach [inp sz out] entab-tests [ | |
if out <> res: entab/size/all inp sz [ | |
print ['FAIL! 'expected mold out "but got" mold res] | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment