Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add UniversalDetector::contains_escape?

  • Loading branch information...
commit 3217c70faea957a6ceaa3f0e59457e53ec7d60b3 1 parent 3c697a3
@CraigCottingham authored
Showing with 40 additions and 2 deletions.
  1. +11 −2 lib/UniversalDetector.rb
  2. +29 −0 spec/universal_detector_spec.rb
View
13 lib/UniversalDetector.rb
@@ -75,7 +75,7 @@ class Detector
def initialize
# @_highBitDetector = Regexp.new('[\x80-\xFF]', nil, 'n')
- @_escDetector = /\033|~\{/
+ # @_escDetector = /\033|~\{/
@_mEscCharSetProber = nil
@_mCharSetProbers = []
reset
@@ -96,6 +96,15 @@ def reset
end
end
+ def contains_escape?(data)
+ return true if data.include? 0x1B
+
+ idx = data.find_index 0x7E
+ return (data[idx + 1] == 0x7B) if idx
+
+ false
+ end
+
def contains_high_bit?(data)
data.any? { | b | (b & 0x80) != 0 }
end
@@ -138,7 +147,7 @@ def feed(data)
if @_mInputState == :PureAscii
if contains_high_bit?(data)
@_mInputState = :Highbyte
- elsif (@_mLastChar + data) =~ @_escDetector
+ elsif contains_escape?(@_mLastChar + data)
@_mInputState = :EscAscii
end
end
View
29 spec/universal_detector_spec.rb
@@ -17,4 +17,33 @@
u.contains_high_bit?([ 0x00, 0x7F ]).should eq(false)
end
+end
+
+describe UniversalDetector, '#contains_escape?' do
+
+ it 'returns true if data contains an escape character' do
+ u = UniversalDetector::Detector.instance
+ u.contains_escape?([ 0x77, 0x30, 0x30, 0x1B, 0x74 ]).should eq(true)
+ end
+
+ it 'returns true if data contains an escape sequence' do
+ u = UniversalDetector::Detector.instance
+ u.contains_escape?([ 0x77, 0x30, 0x30, 0x7E, 0x7B, 0x74 ]).should eq(true)
+ end
+
+ it 'returns true if data contains more than one escape character' do
+ u = UniversalDetector::Detector.instance
+ u.contains_escape?([ 0x77, 0x1B, 0x30, 0x30, 0x1B, 0x74 ]).should eq(true)
+ end
+
+ it 'returns true is data contains more than one escape sequence' do
+ u = UniversalDetector::Detector.instance
+ u.contains_escape?([ 0x77, 0x7E, 0x7B, 0x30, 0x30, 0x7E, 0x7B, 0x74 ]).should eq(true)
+ end
+
+ it 'returns false if data does not contain any escape characters or sequences' do
+ u = UniversalDetector::Detector.instance
+ u.contains_escape?([ 0x77, 0x30, 0x30, 0x74 ]).should eq(false)
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.