Last active
May 6, 2019 01:47
-
-
Save windwiny/b823832bf13d936bf2a506828f2fa749 to your computer and use it in GitHub Desktop.
expandtabs ruby
This file contains hidden or 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
class String | |
# like python expandtabs | |
def expandtabs tabsize=8 | |
self.gsub(/[^\r\n]*/) do |line| | |
next line if line == '' | |
ss = line.split("\t", -1) | |
next line if ss.size == 1 | |
sa = [] | |
lasts = ss.pop | |
ss.each do |s| | |
sa << s | |
m = s.b.size % tabsize # FIXME. graph width 2, but utf-8 byte 3/4 | |
sa << ' ' * ( tabsize - m ) | |
end | |
sa << lasts | |
sa.join | |
end | |
end unless defined? expandtabs | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment