Class: Yast::PunycodeClass
- Inherits:
-
Module
- Object
- Module
- Yast::PunycodeClass
- Defined in:
- ../../src/modules/Punycode.rb
Instance Method Summary (collapse)
-
- (Object) ConvertBackAndForth(strings_in, to_punycode)
Function takes the list of strings and returns them in the converted format.
-
- (Object) CreateNewCacheRecord(decoded, encoded)
Adds new cache records for encoded and decoded strings.
-
- (String) DecodeDomainName(encoded_domain_name)
Decodes the domain name (relative or FQDN) from the Punycode.
-
- (Array<String>) DecodePunycodes(punycode_strings)
Converts list of Punycode strings into their UTF-8 representation.
-
- (Array<String>) DocodeDomainNames(encoded_domain_names)
Decodes the list of domain names to their Unicode representation.
-
- (String) EncodeDomainName(decoded_domain_name)
Encodes the domain name (relative or FQDN) to the Punycode.
-
- (Array<String>) EncodePunycodes(punycode_strings)
Converts list of UTF-8 strings into their Punycode ASCII repserentation.
-
- (String) GetDecodedCachedString(encoded_string)
Returns string encoded in Punycode if it has been already cached.
-
- (String) GetEncodedCachedString(decoded_string)
Returns string encoded in Punycode if it has been already cached.
-
- (Fixnum) GetMaximumCacheSize
Returns the maximum cache size (sum of already converted strings).
-
- (Object) GetTmpDirectory
Returns the current temporary directory.
- - (Object) main
-
- (Object) SetMaximumCacheSize(new_max_size)
Offers to set the maximum cache size (sum of already converted strings).
Instance Method Details
- (Object) ConvertBackAndForth(strings_in, to_punycode)
Function takes the list of strings and returns them in the converted format. Unicode to Punycode or Punycode to Unicode (param to_punycode). It uses a cache of already-converted strings.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File '../../src/modules/Punycode.rb', line 185 def ConvertBackAndForth(strings_in, to_punycode) strings_in = deep_copy(strings_in) # list of returned strings strings_out = [] # Some (or maybe all) strings needn't be cached not_cached = [] # Check the cache for already entered strings current_index = -1 test_cached = Builtins.listmap(strings_in) do |string_in| string_out = nil # Numbers, IPs and empty strings are not converted if Builtins.regexpmatch(string_in, @not_cached_regexp) string_out = string_in else if to_punycode string_out = GetEncodedCachedString(string_in) else string_out = GetDecodedCachedString(string_in) end end if string_out.nil? current_index = Ops.add(current_index, 1) Ops.set(not_cached, current_index, string_in) end { string_in => string_out } end converted_not_cached = [] # There is something not cached, converting them at once if not_cached != [] tmp_in = Ops.add(GetTmpDirectory(), "/tmp-idnconv-in.ycp") tmp_out = Ops.add(GetTmpDirectory(), "/tmp-idnconv-out.ycp") SCR.Write(path(".target.ycp"), tmp_in, not_cached) convert_command = Builtins.sformat( "/usr/bin/idnconv %1 %2 > %3", to_punycode ? "" : "-reverse", tmp_in, tmp_out ) if Convert.to_integer( SCR.Execute(path(".target.bash"), convert_command) ) != 0 Builtins.y2error("Conversion failed!") else converted_not_cached = Convert.convert( SCR.Read(path(".target.ycp"), tmp_out), from: "any", to: "list <string>" ) # Parsing the YCP file failed if converted_not_cached.nil? Builtins.y2error( "Erroneous YCP file: %1", SCR.Read(path(".target.string"), tmp_out) ) end end end # Listing through the given list and adjusting the return list current_index = -1 found_index = -1 Builtins.foreach(strings_in) do |string_in| current_index = Ops.add(current_index, 1) # Already cached string if !Ops.get(test_cached, string_in).nil? Ops.set( strings_out, current_index, Ops.get(test_cached, string_in, "") ) # Recently converted strings else found_index = Ops.add(found_index, 1) Ops.set( strings_out, current_index, Ops.get(converted_not_cached, found_index, "") ) # Adding converted strings to cache if to_punycode CreateNewCacheRecord( string_in, Ops.get(converted_not_cached, found_index, "") ) else CreateNewCacheRecord( Ops.get(converted_not_cached, found_index, ""), string_in ) end end end deep_copy(strings_out) end |
- (Object) CreateNewCacheRecord(decoded, encoded)
Adds new cache records for encoded and decoded strings.
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 |
# File '../../src/modules/Punycode.rb', line 89 def CreateNewCacheRecord(decoded, encoded) # Erroneous cache record return if decoded.nil? || encoded.nil? # Already cached return if Builtins.contains(@cache_decoded, decoded) decoded_size = Builtins.size(decoded) encoded_size = Builtins.size(encoded) # Do not store this record if the cache would exceed maximum if Ops.greater_than( Ops.add(Ops.add(@current_cache_size, decoded_size), encoded_size), @maximum_cache_size ) return end @current_cache_size = Ops.add( Ops.add(@current_cache_size, decoded_size), encoded_size ) Ops.set(@cache_decoded, @current_cache_index, decoded) Ops.set(@cache_encoded, @current_cache_index, encoded) @current_cache_index = Ops.add(@current_cache_index, 1) nil end |
- (String) DecodeDomainName(encoded_domain_name)
Decodes the domain name (relative or FQDN) from the Punycode.
DecodeDomainName(“xn–ala-qma83eb.xn–jlinka-3mb.go.home”) -> “žížala.jůlinka.go.home”
332 333 334 335 336 337 |
# File '../../src/modules/Punycode.rb', line 332 def DecodeDomainName(encoded_domain_name) Builtins.mergestring( DecodePunycodes(Builtins.splitstring(encoded_domain_name, ".")), "." ) end |
- (Array<String>) DecodePunycodes(punycode_strings)
Converts list of Punycode strings into their UTF-8 representation.
304 305 306 307 |
# File '../../src/modules/Punycode.rb', line 304 def DecodePunycodes(punycode_strings) punycode_strings = deep_copy(punycode_strings) ConvertBackAndForth(punycode_strings, false) end |
- (Array<String>) DocodeDomainNames(encoded_domain_names)
Decodes the list of domain names to their Unicode representation. This function is similar to DecodePunycodes but it works with every string as a domain name (that means every domain name is parsed by dots and separately evaluated).
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File '../../src/modules/Punycode.rb', line 350 def DocodeDomainNames(encoded_domain_names) encoded_domain_names = deep_copy(encoded_domain_names) decoded_domain_names = [] strings_to_decode = [] # $[0 : [0, 2], 1 : [3, 5]] backward_map_of_conversion = {} current_domain_index = -1 current_domain_item = 0 # parsing all domain names one by one Builtins.foreach(encoded_domain_names) do |one_domain_name| current_domain_index = Ops.add(current_domain_index, 1) start = current_domain_item # parsing the domain name by dots Builtins.foreach(Builtins.splitstring(one_domain_name, ".")) do |domain_item| Ops.set(strings_to_decode, current_domain_item, domain_item) current_domain_item = Ops.add(current_domain_item, 1) end # creating backward index Ops.set( backward_map_of_conversion, current_domain_index, [start, Ops.subtract(current_domain_item, 1)] ) end # Transformating strings to the decoded format strings_to_decode = DecodePunycodes(strings_to_decode) current_domain_index = -1 Builtins.foreach(encoded_domain_names) do |one_encoded| current_domain_index = Ops.add(current_domain_index, 1) # Where the current string starts and ends current = Ops.get(backward_map_of_conversion, [current_domain_index, 0]) end_ = Ops.get(backward_map_of_conversion, [current_domain_index, 1]) # error? if current.nil? || end_.nil? Builtins.y2error( "Cannot find start/end for %1 in %2", one_encoded, Ops.get(backward_map_of_conversion, current_domain_index) ) Ops.set(decoded_domain_names, current_domain_index, one_encoded) else # create a list of items of the current domain (translated) decoded_domain = [] while Ops.less_or_equal(current, end_) decoded_domain = Builtins.add( decoded_domain, Ops.get(strings_to_decode, current, "") ) current = Ops.add(current, 1) end # create a domain name from these strings Ops.set( decoded_domain_names, current_domain_index, Builtins.mergestring(decoded_domain, ".") ) end end deep_copy(decoded_domain_names) end |
- (String) EncodeDomainName(decoded_domain_name)
Encodes the domain name (relative or FQDN) to the Punycode.
EncodeDomainName(“žížala.jůlinka.go.home”) -> “xn–ala-qma83eb.xn–jlinka-3mb.go.home”
317 318 319 320 321 322 |
# File '../../src/modules/Punycode.rb', line 317 def EncodeDomainName(decoded_domain_name) Builtins.mergestring( EncodePunycodes(Builtins.splitstring(decoded_domain_name, ".")), "." ) end |
- (Array<String>) EncodePunycodes(punycode_strings)
Converts list of UTF-8 strings into their Punycode ASCII repserentation.
294 295 296 297 |
# File '../../src/modules/Punycode.rb', line 294 def EncodePunycodes(punycode_strings) punycode_strings = deep_copy(punycode_strings) ConvertBackAndForth(punycode_strings, true) end |
- (String) GetDecodedCachedString(encoded_string)
Returns string encoded in Punycode if it has been already cached. Returns nil if not found.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File '../../src/modules/Punycode.rb', line 150 def GetDecodedCachedString(encoded_string) ret = nil # numbers and empty strings are not converted if Builtins.regexpmatch(encoded_string, @not_cached_regexp) return encoded_string end counter = -1 # searching through encoded strings to find the index Builtins.foreach(@cache_encoded) do |cached_string| counter = Ops.add(counter, 1) if cached_string == encoded_string # returning decoded representation ret = Ops.get(@cache_decoded, counter) raise Break end end ret end |
- (String) GetEncodedCachedString(decoded_string)
Returns string encoded in Punycode if it has been already cached. Returns nil if not found.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File '../../src/modules/Punycode.rb', line 123 def GetEncodedCachedString(decoded_string) ret = nil # numbers and empty strings are not converted if Builtins.regexpmatch(decoded_string, @not_cached_regexp) return decoded_string end counter = -1 # searching through decoded strings to find the index Builtins.foreach(@cache_decoded) do |cached_string| counter = Ops.add(counter, 1) if cached_string == decoded_string # returning encoded representation ret = Ops.get(@cache_encoded, counter) raise Break end end ret end |
- (Fixnum) GetMaximumCacheSize
Returns the maximum cache size (sum of already converted strings).
66 67 68 |
# File '../../src/modules/Punycode.rb', line 66 def GetMaximumCacheSize @maximum_cache_size end |
- (Object) GetTmpDirectory
Returns the current temporary directory. Lazy loading for the initialization is used.
174 175 176 177 178 179 180 |
# File '../../src/modules/Punycode.rb', line 174 def GetTmpDirectory if @tmp_dir.nil? @tmp_dir = Convert.to_string(SCR.Read(path(".target.tmpdir"))) end @tmp_dir end |
- (Object) main
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File '../../src/modules/Punycode.rb', line 36 def main textdomain "base" @tmp_dir = nil # string, matching this regexp, is not cached @not_cached_regexp = "^[0123456789.]*$" # # Encoded string in cache has the same index # as its Decoded format in the second list. # # list of encoded strings to be cached (Punycode or Unicode) @cache_encoded = [] # list of decoded strings to be cached (Unicode or Punycode) @cache_decoded = [] @current_cache_index = 0 # cached amount of data should be controled @current_cache_size = 0 @maximum_cache_size = 32_768 end |
- (Object) SetMaximumCacheSize(new_max_size)
Offers to set the maximum cache size (sum of already converted strings).
75 76 77 78 79 80 81 82 83 |
# File '../../src/modules/Punycode.rb', line 75 def SetMaximumCacheSize(new_max_size) if !new_max_size.nil? @maximum_cache_size = new_max_size else Builtins.y2error("Cannot set MaximumCacheSize to nil!") end nil end |