quixoten-puppetdb-terminus 3.2.2 → 3.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea438abd4b55566e0db19975b6a7a251e09c39aa
4
- data.tar.gz: adaba8f9743a0fa2c135f46dfdc3fd56af9d8564
3
+ metadata.gz: 8e45da9f15369e2e8dc3a59116c008e4b07be912
4
+ data.tar.gz: 4a0c080aec986122dc8abd2a4d276e6242cfd140
5
5
  SHA512:
6
- metadata.gz: a46eb91ac0a9c4d1c402ab1d64e42886b394bb97391280a7d73f1baab2ac1bfb3265d275d4e06a1cfce35f6e1e375b31358d47ddfe484345e7f1d3b1cbacc3df
7
- data.tar.gz: 941b40c315cdea6bba242560a95edfe7917d5c62d1d6fd8f3eea0bb52a3b7bd7e32f09ade82556a8cdc907dd6d0b0f7f25a4b636aa3774adbee2cfa65def4c7b
6
+ metadata.gz: 94b4278ca6f334d9177a9945f4b8f3fe8c514eca44a78160ac9001584e076fb72f3bee47c2b63536c74cfada4445abf765abe9d469418580555d4e36462c8944
7
+ data.tar.gz: f7125969eb043f116f6ce808106b0c2267860fda85d7cfc09ca293f2745b584dbd0830fc44680f0a66a8c94357c17c029c31b720ebf5d9ba0db62944703f897a
@@ -32,66 +32,48 @@ module CharEncoding
32
32
  Utf8ReplacementChar = [ 0xEF, 0xBF, 0xBD ].pack("c*")
33
33
 
34
34
  DEFAULT_INVALID_CHAR = "\ufffd"
35
-
36
- # @api private
37
- def self.all_indexes_of_char(str, char)
38
- (0..str.length).find_all{ |i| str[i] == char}
39
- end
35
+ NOT_INVALID_REGEX = Regexp.new( "[^" + DEFAULT_INVALID_CHAR + "]" )
40
36
 
41
37
  # @api private
42
38
  #
43
- # Takes an array and returns a sub-array without the last element
39
+ # Finds the beginning and ending index of the first block of invalid
40
+ # characters.
44
41
  #
45
- # @return [Object]
46
- def self.drop_last(array)
47
- array[0..-2]
48
- end
49
-
50
- # @api private
51
- #
52
- # Takes an array of increasing integers and collapses the sequential
53
- # integers into ranges
54
- #
55
- # @param index_array an array of sorted integers
56
- # @return [Range]
57
- def self.collapse_ranges(index_array)
58
- ranges = index_array.each.inject([]) do |spans, n|
59
- if spans.empty? || spans.last.end != n - 1
60
- spans << Range.new(n, n)
61
- else
62
- drop_last(spans) << Range.new(spans.last.begin,n)
63
- end
42
+ # @param str string to scan for invalid characters
43
+ # @return Range
44
+ def self.first_invalid_char_range(str)
45
+ begin_bad_chars_idx = str.index(DEFAULT_INVALID_CHAR)
46
+
47
+ if begin_bad_chars_idx
48
+ first_good_char = str.index(NOT_INVALID_REGEX, begin_bad_chars_idx)
49
+ Range.new(begin_bad_chars_idx, (first_good_char || str.length) - 1)
50
+ else
51
+ nil
64
52
  end
65
53
  end
66
54
 
67
55
  # @api private
68
56
  #
69
- # Scans the string s with bad characters found at bad_char_indexes
70
- # and returns an array of messages that give some context around the
71
- # bad characters. This will give up to 100 characters prior to the
72
- # bad character and 100 after. It will return fewer if it's at the
73
- # beginning of a string or if another bad character appears before
74
- # reaching the 100 characters
57
+ # Scans the string str with invalid characters found at
58
+ # bad_char_range and returns a message that give some context around
59
+ # the bad characters. This will give up to 100 characters prior to
60
+ # the bad character and 100 after. It will return fewer if it's at
61
+ # the beginning of a string or if another bad character appears
62
+ # before reaching the 100 characters
75
63
  #
76
64
  # @param str string coming from to_pson, likely a command to be submitted to PDB
77
- # @param bad_char_indexes an array of indexes into the string where invalid characters were found
78
- # @return [String]
79
- def self.error_char_context(str, bad_char_indexes)
80
- bad_char_ranges = collapse_ranges(bad_char_indexes)
81
- bad_char_ranges.each_with_index.inject([]) do |state, (r, index)|
82
- gap = r.to_a.length
83
-
84
- prev_bad_char_end = bad_char_ranges[index-1].end + 1 if index > 0
85
- next_bad_char_begin = bad_char_ranges[index+1].begin - 1 if index < bad_char_ranges.length - 1
86
-
87
- start_char = [prev_bad_char_end || 0, r.begin-100].max
88
- end_char = [next_bad_char_begin || str.length - 1, r.end+100].min
89
- x = [next_bad_char_begin || str.length, r.end+100, str.length]
90
- prefix = str[start_char..r.begin-1]
91
- suffix = str[r.end+1..end_char]
92
-
93
- state << "'#{prefix}' followed by #{gap} invalid/undefined bytes then '#{suffix}'"
94
- end
65
+ # @param bad_char_range a range indicating a block of invalid characters
66
+ # @return String
67
+ def self.error_char_context(str, bad_char_range)
68
+
69
+ gap = bad_char_range.to_a.length
70
+
71
+ start_char = [0, bad_char_range.begin-100].max
72
+ end_char = [str.index(DEFAULT_INVALID_CHAR, bad_char_range.end+1) || str.length, bad_char_range.end+100].min
73
+ prefix = str[start_char..bad_char_range.begin-1]
74
+ suffix = str[bad_char_range.end+1..end_char-1]
75
+
76
+ "'#{prefix}' followed by #{gap} invalid/undefined bytes then '#{suffix}'"
95
77
  end
96
78
 
97
79
  # @api private
@@ -104,13 +86,14 @@ module CharEncoding
104
86
  # @param error_context_str information about where this string came from for use in error messages
105
87
  # @return String
106
88
  def self.warn_if_invalid_chars(str, error_context_str)
107
- bad_char_indexes = all_indexes_of_char(str, DEFAULT_INVALID_CHAR)
108
- if bad_char_indexes.empty?
89
+
90
+ if str.index(DEFAULT_INVALID_CHAR).nil?
109
91
  str
110
92
  else
111
93
  Puppet.warning "#{error_context_str} ignoring invalid UTF-8 byte sequences in data to be sent to PuppetDB, see debug logging for more info"
94
+
112
95
  if Puppet.settings[:log_level] == "debug"
113
- Puppet.debug error_context_str + "\n" + error_char_context(str, bad_char_indexes).join("\n")
96
+ Puppet.debug error_context_str + "\n" + error_char_context(str, first_invalid_char_range(str))
114
97
  end
115
98
 
116
99
  str
@@ -1,6 +1,6 @@
1
1
  module PuppetDB
2
2
  module Terminus
3
- VERSION = "3.2.2"
3
+ VERSION = "3.2.3"
4
4
  UPSTREAM_VERSION = VERSION.split(".")[0..2].join(".")
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quixoten-puppetdb-terminus
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devin Christensen