Skip to content

Instantly share code, notes, and snippets.

@jhass
Last active March 30, 2017 07:29
Show Gist options
  • Save jhass/abf9a29c6309add414e2b852ac0fff69 to your computer and use it in GitHub Desktop.
Save jhass/abf9a29c6309add414e2b852ac0fff69 to your computer and use it in GitHub Desktop.

Revisions

  1. jhass revised this gist Mar 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion injecting_io.cr
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ class InjectingIO

    first_half = slice
    second_half = nil
    0.upto(slice.size - 1) do |i|
    slice.each_index do |i|
    candidate = slice[i, @token.size]?
    if candidate == @token
    first_half = slice[0, i]?
  2. jhass revised this gist Mar 29, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion injecting_io.cr
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,6 @@ class InjectingIO
    @remaining_token : Bytes?

    def initialize(@io : IO, @token : Bytes, @replacement : Bytes)
    STDERR.puts "Token: #{@token}"
    end

    def read(slice : Bytes)
  3. jhass revised this gist Mar 29, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions injecting_io.cr
    Original file line number Diff line number Diff line change
    @@ -43,9 +43,11 @@ class InjectingIO
    @remaining_token = remaining_token[slice.size, -1]?
    return
    else
    @io.write(@token[0, @token.size - remaining_token.size])
    @remaining_token = nil
    end
    else
    @io.write(@token[0, @token.size - remaining_token.size])
    @remaining_token = nil
    end
    end
    @@ -92,4 +94,6 @@ io.print "o"
    io.puts "o is the coolest"
    io.print "foo"
    io.puts " is okay too"
    io.print "fo"
    io.puts " stays"
    io.close
  4. jhass created this gist Mar 29, 2017.
    95 changes: 95 additions & 0 deletions injecting_io.cr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    struct Slice(T)
    def []?(start, count)
    if start + count >= size
    count = -1
    end

    if count < 0
    count = size - start + (count + 1)
    end

    self[start, count]
    end
    end

    class InjectingIO
    include IO

    getter? closed = false
    @remaining_token : Bytes?

    def initialize(@io : IO, @token : Bytes, @replacement : Bytes)
    STDERR.puts "Token: #{@token}"
    end

    def read(slice : Bytes)
    check_open

    @io.read(slice)
    end

    def write(slice : Bytes)
    check_open

    remaining_token = @remaining_token
    if remaining_token
    if slice[0, remaining_token.size]? == remaining_token
    @io.write(@replacement)
    @io.write(slice[remaining_token.size, -1]?)
    @remaining_token = nil
    return
    elsif slice.size < remaining_token.size
    if slice == remaining_token[0, slice.size]?
    @remaining_token = remaining_token[slice.size, -1]?
    return
    else
    @remaining_token = nil
    end
    else
    @remaining_token = nil
    end
    end

    first_half = slice
    second_half = nil
    0.upto(slice.size - 1) do |i|
    candidate = slice[i, @token.size]?
    if candidate == @token
    first_half = slice[0, i]?
    second_half = slice[i + @token.size, -1]?
    break
    elsif candidate.size < @token.size && candidate == @token[0, candidate.size]?
    @remaining_token = @token[candidate.size, -1]?
    first_half = slice[0, i]?
    break
    end
    end

    @io.write(first_half)

    if second_half
    @io.write(@replacement)
    @io.write(second_half)
    end
    end

    def close
    @io.close
    @closed = true
    end
    end

    io = InjectingIO.new(STDOUT, "foo".to_slice, "bar".to_slice)

    io.puts "Hello World"
    io.puts "Hello foo"
    io.print "fo"
    io.puts "o is cool"
    io.print "f"
    io.puts "oo is cooler"
    io.print "f"
    io.print "o"
    io.puts "o is the coolest"
    io.print "foo"
    io.puts " is okay too"
    io.close