Top Level Namespace

Defined Under Namespace

Modules: Libcouchbase

Instance Method Summary collapse

Instance Method Details

#format_bytes(buf, style = :wide) ⇒ Object



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
# File 'ext/libcouchbase/tools/extract-packets.rb', line 13

def format_bytes(buf, style = :wide)
  out = StringIO.new
  width = style == :wide ? 32 : 16
  full_rows = buf.size / width
  remainder = buf.size % width

  if style == :wide
    out.print("         +-------------------------------------------------------------------------------------------------+\n" \
              "         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |\n" \
              "+--------+-------------------------------------------------------------------------------------------------+--------------------------------+")
  else
    out.print("         +-------------------------------------------------+\n" \
              "         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |\n" \
              "+--------+-------------------------------------------------+----------------+")
  end

  row = 0
  while row < full_rows
    row_start_index = row * width
    # prefix
    out.printf("\n|%08x|", row_start_index)
    row_end_index = row_start_index + width
    # hex
    i = row_start_index
    while i < row_end_index
      out.printf(" %02x", buf[i].ord)
      i += 1
    end
    out.printf(" |")
    # ascii
    i = row_start_index
    while i < row_end_index
      b = buf[i].ord
      i += 1
      if (b <= 0x1f) || (b >= 0x7f)
        out.printf(".")
      else
        out.printf("%c", b)
      end
    end
    out.printf("|")
    row += 1
  end
  if remainder != 0
    row_start_index = full_rows * width
    # prefix
    out.printf("\n|%08x|", row_start_index)
    row_end_index = row_start_index + remainder
    # hex
    i = row_start_index
    while i < row_end_index
      out.printf(" %02x", buf[i].ord)
      i += 1
    end
    i = width - remainder
    while i > 0
      out.printf("   ")
      i -= 1
    end
    out.printf(" |")
    # ascii
    i = row_start_index
    while i < row_end_index
      b = buf[i].ord
      i += 1
      if (b <= 0x1f) || (b >= 0x7f)
        out.printf(".")
      else
        out.printf("%c", b)
      end
    end
    i = width - remainder
    while i > 0
      out.printf(" ")
      i -= 1
    end
    out.printf("|")
  end
  if style == :wide
    out.print("\n+--------+-------------------------------------------------------------------------------------------------+--------------------------------+\n")
  else
    out.print("\n+--------+-------------------------------------------------+----------------+\n")
  end
  out.string
end

#pad(dir, *lines) ⇒ Object



9
10
11
# File 'ext/libcouchbase/tools/extract-packets.rb', line 9

def pad(dir, *lines)
  lines.flatten.join("\n").gsub(/^/, dir == 'snd' ? '> ' : '< ')
end