Class: BufferedTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/buftok.rb

Overview

BufferedTokenizer takes a delimiter upon instantiation, or acts line-based by default. It allows input to be spoon-fed from some outside source which receives arbitrary length datagrams which may-or-may-not contain the token by which entities are delimited. In this respect it’s ideally paired with something like EventMachine (rubyforge.org/projects/eventmachine)

Instance Method Summary collapse

Constructor Details

#initialize(delimiter = "\n") ⇒ BufferedTokenizer

New BufferedTokenizers will operate on lines delimited by “n” by default or allow you to specify any delimiter token you so choose, which will then be used by String#split to tokenize the input data



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/buftok.rb', line 14

def initialize(delimiter = "\n")
  # Store the specified delimiter
  @delimiter = delimiter

  # The input buffer is stored as an array.  This is by far the most efficient
  # approach given language constraints (in C a linked list would be a more
  # appropriate data structure).  Segments of input data are stored in a list
  # which is only joined when a token is reached, substantially reducing the
  # number of objects required for the operation.
  @input = []
end

Instance Method Details

#extract(data) ⇒ Object

Extract takes an arbitrary string of input data and returns an array of tokenized entities, provided there were any available to extract. This makes for easy processing of datagrams using a pattern like:

tokenizer.extract(data).map { |entity| Decode(entity) }.each do ...


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
# File 'lib/buftok.rb', line 31

def extract(data)
  # Extract token-delimited entities from the input string with the split command.
  # There's a bit of craftiness here with the -1 parameter.  Normally split would
  # behave no differently regardless of if the token lies at the very end of the 
  # input buffer or not (i.e. a literal edge case)  Specifying -1 forces split to
  # return "" in this case, meaning that the last entry in the list represents a
  # new segment of data where the token has not been encountered
  entities = data.split @delimiter, -1

  # Move the first entry in the resulting array into the input buffer.  It represents
  # the last segment of a token-delimited entity unless it's the only entry in the list.
  @input << entities.shift

  # If the resulting array from the split is empty, the token was not encountered
  # (not even at the end of the buffer).  Since we've encountered no token-delimited
  # entities this go-around, return an empty array.
  return [] if entities.empty?

  # At this point, we've hit a token, or potentially multiple tokens.  Now we can bring
  # together all the data we've buffered from earlier calls without hitting a token,
  # and add it to our list of discovered entities.
  entities.unshift @input.join

  # Now that we've hit a token, joined the input buffer and added it to the entities
  # list, we can go ahead and clear the input buffer.  All of the segments that were
  # stored before the join can now be garbage collected.
  @input.clear
  
  # The last entity in the list is not token delimited, however, thanks to the -1
  # passed to split.  It represents the beginning of a new list of as-yet-untokenized  
  # data, so we add it to the start of the list.
  @input << entities.pop
  
  # Now we're left with the list of extracted token-delimited entities we wanted
  # in the first place.  Hooray!
  entities
end

#flushObject

Flush the contents of the input buffer, i.e. return the input buffer even though a token has not yet been encountered



71
72
73
74
75
# File 'lib/buftok.rb', line 71

def flush
  buffer = @input.join
  @input.clear
  buffer
end