Module: Vers::APKVersion
Constant Summary collapse
- TOKEN_INITIAL_DIGIT =
0- TOKEN_DIGIT =
1- TOKEN_LETTER =
2- TOKEN_SUFFIX =
3- TOKEN_SUFFIX_NUMBER =
4- TOKEN_COMMIT_HASH =
5- TOKEN_REVISION_NUMBER =
6- TOKEN_END =
7- TOKEN_INVALID =
8- SUFFIXES =
{ "alpha" => 1, "beta" => 2, "pre" => 3, "rc" => 4, "cvs" => 6, "svn" => 7, "git" => 8, "hg" => 9, "p" => 10 }.freeze
- SUFFIX_NONE =
5- UINT64_MASK =
(1 << 64) - 1
Instance Method Summary collapse
- #compare(left, right) ⇒ Object
- #compare_token(left, right) ⇒ Object
- #invalid_tokens(tokens) ⇒ Object
- #normalize(value) ⇒ Object
- #parse(value) ⇒ Object
- #prerelease?(value) ⇒ Boolean
- #scan_digits(value, index) ⇒ Object
- #scan_hex(value, index) ⇒ Object
- #scan_lowercase(value, index) ⇒ Object
- #token_type(tokens, index) ⇒ Object
- #uint64(value) ⇒ Object
- #valid?(value) ⇒ Boolean
Instance Method Details
#compare(left, right) ⇒ Object
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 |
# File 'lib/vers/special_version.rb', line 33 def compare(left, right) left_tokens = parse(left) right_tokens = parse(right) index = 0 while token_type(left_tokens, index) == token_type(right_tokens, index) && token_type(left_tokens, index) < TOKEN_END comparison = compare_token(left_tokens.fetch(index), right_tokens.fetch(index)) return comparison unless comparison.zero? index += 1 end left_type = token_type(left_tokens, index) right_type = token_type(right_tokens, index) return 0 if left_type == right_type left_token = left_tokens[index] right_token = right_tokens[index] return -1 if left_type == TOKEN_SUFFIX && left_token.fetch(1) < SUFFIX_NONE return 1 if right_type == TOKEN_SUFFIX && right_token.fetch(1) < SUFFIX_NONE return -1 if left_type > right_type return 1 if right_type > left_type 0 end |
#compare_token(left, right) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/vers/special_version.rb', line 150 def compare_token(left, right) type = left.fetch(0) left_value = left.fetch(1) right_value = right.fetch(1) case type when TOKEN_DIGIT return left_value <=> right_value if left_value.start_with?("0") || right_value.start_with?("0") uint64(left_value) <=> uint64(right_value) when TOKEN_INITIAL_DIGIT, TOKEN_SUFFIX_NUMBER, TOKEN_REVISION_NUMBER uint64(left_value) <=> uint64(right_value) else left_value <=> right_value end end |
#invalid_tokens(tokens) ⇒ Object
146 147 148 |
# File 'lib/vers/special_version.rb', line 146 def invalid_tokens(tokens) tokens << [TOKEN_INVALID, nil] end |
#normalize(value) ⇒ Object
63 64 65 |
# File 'lib/vers/special_version.rb', line 63 def normalize(value) value.to_s.strip end |
#parse(value) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/vers/special_version.rb', line 74 def parse(value) string = value.to_s.strip tokens = [] first, index = scan_digits(string, 0) return invalid_tokens(tokens) if first.empty? tokens << [TOKEN_INITIAL_DIGIT, first] previous = TOKEN_INITIAL_DIGIT while index < string.length byte = string.getbyte(index) case byte when 97..122 return invalid_tokens(tokens) if previous > TOKEN_DIGIT tokens << [TOKEN_LETTER, byte] previous = TOKEN_LETTER index += 1 when 46 return invalid_tokens(tokens) if previous > TOKEN_DIGIT digits, index = scan_digits(string, index + 1) return invalid_tokens(tokens) if digits.empty? tokens << [TOKEN_DIGIT, digits] previous = TOKEN_DIGIT when 48..57 type = if previous == TOKEN_INITIAL_DIGIT || previous == TOKEN_DIGIT TOKEN_DIGIT elsif previous == TOKEN_SUFFIX TOKEN_SUFFIX_NUMBER end return invalid_tokens(tokens) unless type digits, index = scan_digits(string, index) tokens << [type, digits] previous = type when 95 return invalid_tokens(tokens) if previous > TOKEN_SUFFIX_NUMBER suffix, index = scan_lowercase(string, index + 1) rank = SUFFIXES[suffix] return invalid_tokens(tokens) unless rank tokens << [TOKEN_SUFFIX, rank] previous = TOKEN_SUFFIX when 126 return invalid_tokens(tokens) if previous >= TOKEN_COMMIT_HASH commit, index = scan_hex(string, index + 1) return invalid_tokens(tokens) if commit.empty? tokens << [TOKEN_COMMIT_HASH, commit] previous = TOKEN_COMMIT_HASH when 45 if previous >= TOKEN_REVISION_NUMBER || string[index, 2] != "-r" return invalid_tokens(tokens) end revision, index = scan_digits(string, index + 2) return invalid_tokens(tokens) if revision.empty? tokens << [TOKEN_REVISION_NUMBER, revision] previous = TOKEN_REVISION_NUMBER else return invalid_tokens(tokens) end end tokens end |
#prerelease?(value) ⇒ Boolean
67 68 69 70 71 72 |
# File 'lib/vers/special_version.rb', line 67 def prerelease?(value) tokens = parse(value.to_s.strip) return false if tokens.any? { |type, _value| type == TOKEN_INVALID } tokens.any? { |type, suffix| type == TOKEN_SUFFIX && suffix < SUFFIX_NONE } end |
#scan_digits(value, index) ⇒ Object
175 176 177 178 179 |
# File 'lib/vers/special_version.rb', line 175 def scan_digits(value, index) ending = index ending += 1 while ending < value.length && value.getbyte(ending).between?(48, 57) [value[index...ending], ending] end |
#scan_hex(value, index) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/vers/special_version.rb', line 187 def scan_hex(value, index) ending = index while ending < value.length byte = value.getbyte(ending) break unless byte.between?(48, 57) || byte.between?(65, 70) || byte.between?(97, 102) ending += 1 end [value[index...ending], ending] end |
#scan_lowercase(value, index) ⇒ Object
181 182 183 184 185 |
# File 'lib/vers/special_version.rb', line 181 def scan_lowercase(value, index) ending = index ending += 1 while ending < value.length && value.getbyte(ending).between?(97, 122) [value[index...ending], ending] end |
#token_type(tokens, index) ⇒ Object
167 168 169 |
# File 'lib/vers/special_version.rb', line 167 def token_type(tokens, index) tokens[index]&.fetch(0) || TOKEN_END end |
#uint64(value) ⇒ Object
171 172 173 |
# File 'lib/vers/special_version.rb', line 171 def uint64(value) value.each_byte.reduce(0) { |number, byte| ((number * 10) + byte - 48) & UINT64_MASK } end |
#valid?(value) ⇒ Boolean
59 60 61 |
# File 'lib/vers/special_version.rb', line 59 def valid?(value) parse(value.to_s.strip).none? { |type, _value| type == TOKEN_INVALID } end |