LoginSignup
0
0

More than 5 years have passed since last update.

アンエスケープ

Last updated at Posted at 2018-12-08

1文字ずつがんばる。CoSVONでの経験が生きた。

# そういえばあれって CodeIQ管理者ページの出力するcsvのエスケープが壊れている不具合 を抱えていたんでパーサを再発明する以外になかったんでしたっけ--;;;

tyama_henae29.rb
#!/usr/bin/ruby
#http://nabetani.sakura.ne.jp/hena/orde29unes/
#https://qiita.com/Nabetani/items/f2db9b916c0a301b744f

def solve(s)
    r=[]
    k="".dup
    escape=nil
    i=0
    while i<s.size
        if s[i]=="'"
            if !escape
                escape="'"
            elsif escape=="'"
                escape=nil
            else
                k<<"'"
            end
        elsif s[i]=='"'
            if !escape
                escape='"'
            elsif escape=='"'
                escape=nil
            else
                k<<'"'
            end
        elsif s[i]=="/"
            if escape
                k<<"/"
            elsif s[i+1]=="/"
                i+=1
                k<<"/"
            else
                r<<k
                k="".dup
            end
        else
            k<<s[i]
        end
        i+=1
    end
    r<<k
    if escape || r.any?(&:empty?)
        "-"
    else
        r*","
    end
end

while gets
    puts solve($_.chomp)
    STDOUT.flush
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0