Class: Yast::StringClass
- Inherits:
-
Module
- Object
- Module
- Yast::StringClass
- Defined in:
- ../../src/modules/String.rb
Instance Method Summary (collapse)
-
- (Object) CAlnum
The 62 upper and lowercase ASCII letters and digits.
-
- (Object) CAlpha
The 52 upper and lowercase ASCII letters.
-
- (Object) CDigit
Digits: 0123456789.
-
- (Object) CGraph
Printable ASCII charcters except whitespace, 33-126.
-
- (Object) CLower
The 26 lowercase ASCII letters.
-
- (Object) CPrint
Printable ASCII characters including whitespace.
-
- (Object) CPunct
The ASCII printable non-blank non-alphanumeric characters.
-
- (Object) CreateTableHeaderUnderline(cols_lenghts, horizontal_padding)
-
hidden for documentation -.
-
-
- (Object) CreateTableRow(row_items, cols_lenghts, horizontal_padding)
-
hidden for documentation -.
-
-
- (Object) CreateUnderline(length)
-
hidden for documentation -.
-
-
- (Object) CSpace
ASCII whitespace: SPACE CR LF HT VT FF.
-
- (Object) CUpper
The 26 uppercase ASCII letters.
-
- (Object) CutBlanks(input)
Remove blanks at begin and end of input string.
-
- (String) CutRegexMatch(input, regex, glob)
Remove first or every match of given regular expression from a string.
-
- (String) CutZeros(input)
Remove any leading zeros.
-
- (Object) CXdigit
Hexadecimal digits: 0123456789ABCDEFabcdef.
-
- (String) EscapeTags(text)
Function for escaping (replacing) (HTML|XML…) tags with their (HTML|XML…) meaning.
-
- (Object) FindLongestRecords(items)
-
hidden for documentation -.
-
-
- (Object) FindMountPoint(dir, dirs)
Find a mount point for given directory/file path.
-
- (Object) FirstChunk(s, separators)
Shorthand for select (splitstring (s, separators), 0, “”) Useful now that the above produces a deprecation warning.
-
- (String) FormatFilename(file_path, len)
Format file name - truncate the middle part of the directory to fit to the reqested lenght.
-
- (Object) FormatRate(bytes_per_second)
Return a pretty description of a download rate.
-
- (String) FormatRateMessage(text, avg_bps, curr_bps)
Add a download rate status to a message.
-
- (Object) FormatSize(bytes)
Return a pretty description of a byte count.
-
- (Object) FormatSizeWithPrecision(bytes, precision, omit_zeroes)
Return a pretty description of a byte count.
-
- (String) FormatTime(seconds)
Format an integer seconds value with min:sec or hours:min:sec.
-
- (String) FormatTwoDigits(x)
Format an integer number as (at least) two digits; use leading zeroes if necessary.
-
- (String) GetCommentLines(input)
Get comment without metadata.
-
- (Array<String>) GetMetaDataLines(input)
Get metadata lines from input string.
- - (Object) main
-
- (Object) NewlineItems(s)
The items as a list, with empty lines removed.
-
- (Object) NonEmpty(l)
Only non-“” items.
-
- (Object) OptFormat(f, s)
Optional formatted text.
-
- (Object) OptParens(s)
Optional parenthesized text.
-
- (Object) Pad(text, length)
Add spaces after the text to make it long enough.
-
- (Object) PadZeros(text, length)
Add zeros before the text to make it long enough.
-
- (Array<String>) ParseOptions(options, parameters)
Parse string of values.
-
- (Hash) ParseSysconfigComment(comment)
Parse metadata from a sysconfig comment.
-
- (Object) Quote(var)
Quote a string with 's.
-
- (Object) Random(len)
Make a random base-36 number.
-
- (String) RemoveShortcut(label)
Remove a shortcut from a label, so that it can be inserted into help to avoid risk of different translation of the label.
-
- (String) Repeat(text, number)
Repeat a string.
-
- (String) Replace(s, source, target)
Replace substring in a string.
-
- (Object) ReplaceWith(str, chars, glue)
Replaces all characters in a given string with some other string or character.
-
- (Object) StartsWith(str, test)
Checks whether string str starts with test.
-
- (Object) SuperPad(text, length, padding, alignment)
Add the padding character around the text to make it long enough.
-
- (String) TextTable(header, items, options)
Function creates text table without using HTML tags.
-
- (String) UnderlinedHeader(header_line, left_padding)
Function returns underlined text header without using HTML tags.
-
- (Object) UnQuote(var)
Unquote a string with 's (quoted with quote).
-
- (String) ValidCharsFilename
Characters valid in a filename (not pathname).
-
- (String) WrapAt(text, width, split_string)
Returns text wrapped at defined margin.
-
- (Boolean) YesNo(value)
Value as “Yes” or “No”.
Instance Method Details
- (Object) CAlnum
The 62 upper and lowercase ASCII letters and digits
716 717 718 |
# File '../../src/modules/String.rb', line 716 def CAlnum @calnum end |
- (Object) CAlpha
The 52 upper and lowercase ASCII letters
701 702 703 |
# File '../../src/modules/String.rb', line 701 def CAlpha @calpha end |
- (Object) CDigit
Digits: 0123456789
706 707 708 |
# File '../../src/modules/String.rb', line 706 def CDigit @cdigit end |
- (Object) CGraph
Printable ASCII charcters except whitespace, 33-126
726 727 728 |
# File '../../src/modules/String.rb', line 726 def CGraph @cgraph end |
- (Object) CLower
The 26 lowercase ASCII letters
696 697 698 |
# File '../../src/modules/String.rb', line 696 def CLower @clower end |
- (Object) CPrint
Printable ASCII characters including whitespace
736 737 738 |
# File '../../src/modules/String.rb', line 736 def CPrint @cprint end |
- (Object) CPunct
The ASCII printable non-blank non-alphanumeric characters
721 722 723 |
# File '../../src/modules/String.rb', line 721 def CPunct @cpunct end |
- (Object) CreateTableHeaderUnderline(cols_lenghts, horizontal_padding)
-
hidden for documentation -
Local function for creating header underline for table. It uses maximal lengths of records defined in cols_lenghts.
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 |
# File '../../src/modules/String.rb', line 829 def CreateTableHeaderUnderline(cols_lenghts, horizontal_padding) cols_lenghts = deep_copy(cols_lenghts) col_counter = 0 # count of added paddings records_count = Ops.subtract(Builtins.size(cols_lenghts), 1) # total length of underline total_size = 0 Builtins.foreach(cols_lenghts) do |col_size| total_size = Ops.add(total_size, col_size) # adding padding where necessary if Ops.less_than(col_counter, records_count) total_size = Ops.add(total_size, horizontal_padding) end col_counter = Ops.add(col_counter, 1) end CreateUnderline(total_size) end |
- (Object) CreateTableRow(row_items, cols_lenghts, horizontal_padding)
-
hidden for documentation -
Local function creates table row.
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
# File '../../src/modules/String.rb', line 785 def CreateTableRow(row_items, cols_lenghts, horizontal_padding) row_items = deep_copy(row_items) cols_lenghts = deep_copy(cols_lenghts) row = "" col_counter = 0 records_count = Ops.subtract(Builtins.size(row_items), 1) Builtins.foreach(row_items) do |record| padding = Ops.get(cols_lenghts, col_counter, 0) if Ops.less_than(col_counter, records_count) padding = Ops.add(padding, horizontal_padding) end row = Ops.add(row, Pad(record, padding)) col_counter = Ops.add(col_counter, 1) end row end |
- (Object) CreateUnderline(length)
-
hidden for documentation -
Local function returns underline string /length/ long.
811 812 813 814 815 816 817 818 819 |
# File '../../src/modules/String.rb', line 811 def CreateUnderline(length) underline = @base_underline while Ops.less_than(Builtins.size(underline), length) underline = Ops.add(underline, @base_underline) end underline = Builtins.substring(underline, 0, length) underline end |
- (Object) CSpace
ASCII whitespace: SPACE CR LF HT VT FF
731 732 733 |
# File '../../src/modules/String.rb', line 731 def CSpace @cspace end |
- (Object) CUpper
The 26 uppercase ASCII letters
691 692 693 |
# File '../../src/modules/String.rb', line 691 def CUpper @cupper end |
- (Object) CutBlanks(input)
Remove blanks at begin and end of input string.
316 317 318 319 320 321 322 323 324 325 |
# File '../../src/modules/String.rb', line 316 def CutBlanks(input) return "" if input == nil || Ops.less_than(Builtins.size(input), 1) pos1 = Builtins.findfirstnotof(input, " \t") return "" if pos1 == nil pos2 = Builtins.findlastnotof(input, " \t") Builtins.substring(input, pos1, Ops.add(Ops.subtract(pos2, pos1), 1)) end |
- (String) CutRegexMatch(input, regex, glob)
Remove first or every match of given regular expression from a string
(e.g. CutRegexMatch( “abcdef12ef34gh000”, “[0-9]+”, true ) -> “abcdefefgh”, CutRegexMatch( "abcdef12ef34gh000", "[0-9]+", false ) -> "abcdefef34gh000")
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File '../../src/modules/String.rb', line 646 def CutRegexMatch(input, regex, glob) return "" if input == nil || Ops.less_than(Builtins.size(input), 1) output = input if Builtins.regexpmatch(output, regex) p = Builtins.regexppos(output, regex) begin output = Ops.add( Builtins.substring(output, 0, Ops.get_integer(p, 0, 0)), Builtins.substring( output, Ops.add(Ops.get_integer(p, 0, 0), Ops.get_integer(p, 1, 0)) ) ) p = Builtins.regexppos(output, regex) end while glob && Ops.greater_than(Builtins.size(p), 0) end output end |
- (String) CutZeros(input)
Remove any leading zeros
Remove any leading zeros that make tointeger inadvertently assume an octal number (e.g. "09" -> "9", "0001" -> "1", but "0" -> "0")
335 336 337 338 339 340 341 |
# File '../../src/modules/String.rb', line 335 def CutZeros(input) return "" if input == nil || Ops.less_than(Builtins.size(input), 1) return input if !Builtins.regexpmatch(input, "^0.*") output = Builtins.regexpsub(input, "^0+(.*)$", "\\1") return "0" if Ops.less_than(Builtins.size(output), 1) output end |
- (Object) CXdigit
Hexadecimal digits: 0123456789ABCDEFabcdef
711 712 713 |
# File '../../src/modules/String.rb', line 711 def CXdigit @cxdigit end |
- (String) EscapeTags(text)
Function for escaping (replacing) (HTML|XML…) tags with their (HTML|XML...) meaning.
Usable to present text “as is” in RichText.
672 673 674 675 676 677 678 |
# File '../../src/modules/String.rb', line 672 def EscapeTags(text) text = Builtins.mergestring(Builtins.splitstring(text, "&"), "&") text = Builtins.mergestring(Builtins.splitstring(text, "<"), "<") text = Builtins.mergestring(Builtins.splitstring(text, ">"), ">") text end |
- (Object) FindLongestRecords(items)
-
hidden for documentation -
Local function for finding longest records in the table.
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 |
# File '../../src/modules/String.rb', line 754 def FindLongestRecords(items) items = deep_copy(items) longest = [] # searching all rows Builtins.foreach(items) do |row| # starting with column 0 col_counter = 0 # testing all columns on the row Builtins.foreach(row) do |col| col_size = Builtins.size(col) # found longer record for this column if Ops.greater_than(col_size, Ops.get(longest, col_counter, -1)) Ops.set(longest, col_counter, col_size) end # next column col_counter = Ops.add(col_counter, 1) end end deep_copy(longest) end |
- (Object) FindMountPoint(dir, dirs)
Find a mount point for given directory/file path. Returns “/” if no mount point matches
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 |
# File '../../src/modules/String.rb', line 1299 def FindMountPoint(dir, dirs) dirs = deep_copy(dirs) while dir != nil && dir != "" && !Builtins.contains(dirs, dir) # strip the last path component and try it again comps = Builtins.splitstring(dir, "/") comps = Builtins.remove(comps, Ops.subtract(Builtins.size(comps), 1)) dir = Builtins.mergestring(comps, "/") end dir = "/" if dir == nil || dir == "" dir end |
- (Object) FirstChunk(s, separators)
Shorthand for select (splitstring (s, separators), 0, “”) Useful now that the above produces a deprecation warning.
685 686 687 688 |
# File '../../src/modules/String.rb', line 685 def FirstChunk(s, separators) l = Builtins.splitstring(s, separators) Ops.get(l, 0, "") end |
- (String) FormatFilename(file_path, len)
Format file name - truncate the middle part of the directory to fit to the reqested lenght. Path elements in the middle of the string are replaced by ellipsis (...). The result migth be longer that requested size if size of the last element (with ellipsis) is longer than the requested size. If the requested size is greater than size of the input then the string is not modified. The last part (file name) is never removed.
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 |
# File '../../src/modules/String.rb', line 1229 def FormatFilename(file_path, len) return file_path if Ops.less_or_equal(Builtins.size(file_path), len) dir = Builtins.splitstring(file_path, "/") file = Ops.get(dir, Ops.subtract(Builtins.size(dir), 1), "") dir = Builtins.remove(dir, Ops.subtract(Builtins.size(dir), 1)) # there is a slash at the end, add the directory name if file == "" file = Ops.add( Ops.get(dir, Ops.subtract(Builtins.size(dir), 1), ""), "/" ) dir = Builtins.remove(dir, Ops.subtract(Builtins.size(dir), 1)) end if Ops.less_or_equal(Builtins.size(Builtins.mergestring(dir, "/")), 3) || Builtins.size(dir) == 0 # the path is short, replacing by ... cannot help return file_path end ret = "" begin # put the ellipsis in the middle of the path ellipsis = Ops.divide(Builtins.size(dir), 2) # ellipsis - used to replace part of text to make it shorter # example: "/really/very/long/file/name", "/.../file/name") Ops.set(dir, ellipsis, _("...")) ret = Builtins.mergestring(Builtins.add(dir, file), "/") if Ops.greater_than(Builtins.size(ret), len) # still too long, remove the ellipsis and start a new iteration dir = Builtins.remove(dir, ellipsis) else # the size is OK break end end while Ops.greater_than(Builtins.size(dir), 0) ret end |
- (Object) FormatRate(bytes_per_second)
Return a pretty description of a download rate
Return a pretty description of a download rate, with two fraction digits and using B/s, KiB/s, MiB/s, GiB/s or TiB/s as unit as appropriate.
235 236 237 238 239 |
# File '../../src/modules/String.rb', line 235 def FormatRate(bytes_per_second) # covert a number to download rate string # %1 is string - size in bytes, B, KiB, MiB, GiB or TiB Builtins.sformat(_("%1/s"), FormatSize(bytes_per_second)) end |
- (String) FormatRateMessage(text, avg_bps, curr_bps)
Add a download rate status to a message.
Add the current and the average download rate to the message.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File '../../src/modules/String.rb', line 250 def FormatRateMessage(text, avg_bps, curr_bps) rate = "" if Ops.greater_than(curr_bps, 0) rate = FormatRate(curr_bps) if Ops.greater_than(avg_bps, 0) # format download rate message: %1 = the current download rate (e.g. "242.6kB/s") # %2 is the average download rate (e.g. "228.3kB/s") # to translators: keep translation of "on average" as short as possible rate = Builtins.sformat( _("%1 (on average %2)"), rate, FormatRate(avg_bps) ) end end # add download rate to the downloading message # %1 is URL, %2 is formatted download rate, e.g. "242.6kB/s (avg. 228.3kB/s)" # in ncurses UI the strings are exchanged (%1 is the rate, %2 is URL) # due to limited space on the screen Builtins.sformat(text, rate) end |
- (Object) FormatSize(bytes)
Return a pretty description of a byte count
Return a pretty description of a byte count, with two fraction digits and using B, KiB, MiB, GiB or TiB as unit as appropriate.
Uses the current locale defined decimal separator (i.e. the result is language dependant).
217 218 219 220 221 222 |
# File '../../src/modules/String.rb', line 217 def FormatSize(bytes) return "" if bytes == nil # automatic precision, don't print trailing zeroes for sizes < 1MiB FormatSizeWithPrecision(bytes, -1, Ops.less_than(bytes, 1 << 20)) end |
- (Object) FormatSizeWithPrecision(bytes, precision, omit_zeroes)
Return a pretty description of a byte count
Return a pretty description of a byte count with required precision and using B, KiB, MiB, GiB or TiB as unit as appropriate.
Uses the current locale defined decimal separator (i.e. the result is language dependant).
(useful for memory size - 128 MiB RAM looks better than 128.00 MiB RAM)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File '../../src/modules/String.rb', line 139 def FormatSizeWithPrecision(bytes, precision, omit_zeroes) return "" if bytes == nil units = [ # Byte abbreviated _("B"), # KiloByte abbreviated _("KiB"), # MegaByte abbreviated _("MiB"), # GigaByte abbreviated _("GiB"), # TeraByte abbreviated _("TiB") ] index = 0 whole = Builtins.tofloat(bytes) while (Ops.greater_or_equal(whole, 1024.0) || Ops.less_or_equal(whole, -1024.0)) && Ops.less_than(Ops.add(index, 1), Builtins.size(units)) whole = Ops.divide(whole, 1024.0) index = Ops.add(index, 1) end if precision == nil precision = 0 elsif Ops.less_than(precision, 0) # auto precision - depends on the suffix, but max. 3 decimal digits precision = Ops.less_or_equal(index, 3) ? index : 3 end if omit_zeroes == true max_difference = 0.9 i = precision while Ops.greater_than(i, 0) max_difference = Ops.divide( max_difference, Convert.convert(10, :from => "integer", :to => "float") ) i = Ops.subtract(i, 1) end if Ops.less_than( Ops.subtract( whole, Convert.convert( Builtins.tointeger(whole), :from => "integer", :to => "float" ) ), max_difference ) precision = 0 end end Ops.add( Ops.add(Builtins::Float.tolstring(whole, precision), " "), Ops.get_string(units, index, "") ) end |
- (String) FormatTime(seconds)
Format an integer seconds value with min:sec or hours:min:sec
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File '../../src/modules/String.rb', line 291 def FormatTime(seconds) return "" if Ops.less_than(seconds, 0) if Ops.less_than(seconds, 3600) # Less than one hour return Builtins.sformat( "%1:%2", FormatTwoDigits(Ops.divide(seconds, 60)), FormatTwoDigits(Ops.modulo(seconds, 60)) ) # More than one hour - we don't hope this will ever happen, but who knows? else hours = Ops.divide(seconds, 3600) seconds = Ops.modulo(seconds, 3600) return Builtins.sformat( "%1:%2:%3", hours, FormatTwoDigits(Ops.divide(seconds, 60)), FormatTwoDigits(Ops.modulo(seconds, 60)) ) end end |
- (String) FormatTwoDigits(x)
Format an integer number as (at least) two digits; use leading zeroes if necessary.
280 281 282 283 284 |
# File '../../src/modules/String.rb', line 280 def FormatTwoDigits(x) Ops.less_than(x, 10) && Ops.greater_or_equal(x, 0) ? Builtins.sformat("0%1", x) : Builtins.sformat("%1", x) end |
- (String) GetCommentLines(input)
Get comment without metadata
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File '../../src/modules/String.rb', line 947 def GetCommentLines(input) return "" if input == nil || input == "" lines = Builtins.splitstring(input, "\n") ret = "" Builtins.foreach(lines) do |line| com_line = Builtins.regexpsub(line, "^#([^#].*)", "\\1") if com_line == nil # add empty lines if Builtins.regexpmatch(line, "^#[ \t]*$") == true ret = Ops.add(ret, "\n") end else ret = Ops.add(Ops.add(ret, com_line), "\n") end end ret end |
- (Array<String>) GetMetaDataLines(input)
Get metadata lines from input string
937 938 939 940 941 942 |
# File '../../src/modules/String.rb', line 937 def GetMetaDataLines(input) return [] if input == nil || input == "" lines = Builtins.splitstring(input, "\n") Builtins.filter(lines) { |line| Builtins.regexpmatch(line, "^##.*") } end |
- (Object) main
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File '../../src/modules/String.rb', line 34 def main textdomain "base" # character sets, suitable for ValidChars @cupper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" @clower = "abcdefghijklmnopqrstuvwxyz" @calpha = Ops.add(@cupper, @clower) @cdigit = "0123456789" @cxdigit = Ops.add(@cdigit, "ABCDEFabcdef") @calnum = Ops.add(@calpha, @cdigit) @cpunct = "!\"\#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" @cgraph = Ops.add(@calnum, @cpunct) @cspace = " \n\t" @cprint = Ops.add(@cspace, @cgraph) # 64 characters is the base undeline length @base_underline = "----------------------------------------------------------------" end |
- (Object) NewlineItems(s)
Returns the items as a list, with empty lines removed
103 104 105 |
# File '../../src/modules/String.rb', line 103 def NewlineItems(s) NonEmpty(Builtins.splitstring(s, "\n")) end |
- (Object) NonEmpty(l)
Returns only non-“” items
96 97 98 99 |
# File '../../src/modules/String.rb', line 96 def NonEmpty(l) l = deep_copy(l) Builtins.filter(l) { |i| i != "" } end |
- (Object) OptFormat(f, s)
Optional formatted text
84 85 86 |
# File '../../src/modules/String.rb', line 84 def OptFormat(f, s) s == "" || s == nil ? "" : Builtins.sformat(f, s) end |
- (Object) OptParens(s)
Optional parenthesized text
90 91 92 |
# File '../../src/modules/String.rb', line 90 def OptParens(s) OptFormat(" (%1)", s) end |
- (Object) Pad(text, length)
Add spaces after the text to make it long enough
Add spaces after the text to make it long enough. If the text is longer than requested, no changes are made.
396 397 398 |
# File '../../src/modules/String.rb', line 396 def Pad(text, length) SuperPad(text, length, " ", :left) end |
- (Object) PadZeros(text, length)
Add zeros before the text to make it long enough.
Add zeros before the text to make it long enough. If the text is longer than requested, no changes are made.
408 409 410 |
# File '../../src/modules/String.rb', line 408 def PadZeros(text, length) SuperPad(text, length, "0", :right) end |
- (Array<String>) ParseOptions(options, parameters)
Parse string of values
Parse string of values - split string to values, quoting and backslash sequences are supported "separator":<string> - value separator (default: " \t"), "unique":<boolean> - result will not contain any duplicates, first occurance of the string is stored into output (default: false), "interpret_backslash":<boolean> - convert backslash sequence into one character (e.g. "\n" => "\n") (default: true) "remove_whitespace":<boolean> - remove white spaces around values (default: true),
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
# File '../../src/modules/String.rb', line 422 def ParseOptions(, parameters) parameters = deep_copy(parameters) ret = [] # parsing options separator = Ops.get_string(parameters, "separator", " \t") unique = Ops.get_boolean(parameters, "unique", false) interpret_backslash = Ops.get_boolean( parameters, "interpret_backslash", true ) remove_whitespace = Ops.get_boolean(parameters, "remove_whitespace", true) Builtins.y2debug( "Input: string: '%1', parameters: %2", , parameters ) Builtins.y2debug( "Used values: separator: '%1', unique: %2, remove_whitespace: %3", separator, unique, remove_whitespace ) return [] if == nil # two algorithms are used: # first is much faster, but only usable if string # doesn't contain any double qoute characters # and backslash sequences are not interpreted # second is more general, but of course slower if Builtins.findfirstof(, "\"") == nil && interpret_backslash == false # easy case - no qouting, don't interpres backslash sequences => use splitstring values = Builtins.splitstring(, separator) Builtins.foreach(values) do |v| v = CutBlanks(v) if remove_whitespace == true if unique == true ret = Builtins.add(ret, v) if !Builtins.contains(ret, v) else ret = Builtins.add(ret, v) end end else # quoting is used or backslash interpretation is enabled # so it' not possible to split input # parsing each character is needed - use finite automaton # state state = :out_of_string # position in the input string index = 0 # parsed value - buffer str = "" while Ops.less_than(index, Builtins.size()) character = Builtins.substring(, index, 1) Builtins.y2debug( "character: %1 state: %2 index: %3", character, state, index ) # interpret backslash sequence if character == "\\" && interpret_backslash == true if Ops.less_than(Ops.add(index, 1), Builtins.size()) nextcharacter = Builtins.substring(, Ops.add(index, 1), 1) index = Ops.add(index, 1) # backslah sequences backslash_seq = { "a" => "a", # alert "b" => "", # backspace "e" => "e", # escape "f" => "", # FF "n" => "\n", # NL "r" => " ", # CR "t" => "\t", # tab "v" => "v", # vertical tab "\\" => "\\" } # backslash if Builtins.haskey(backslash_seq, nextcharacter) == true character = Ops.get_string( backslash_seq, nextcharacter, "DUMMY" ) else if nextcharacter != "\"" # ignore backslash in invalid backslash sequence character = nextcharacter else # backslash will be removed later, # double quote and escaped double quote have to different yet character = "\\\"" end end Builtins.y2debug("backslash sequence: '%1'", character) else Builtins.y2warning( "Missing character after backslash (\\) at the end of string" ) end end if state == :out_of_string # ignore separator or white space at the beginning of the string if Builtins.issubstring(separator, character) == true || remove_whitespace == true && (character == " " || character == "\t") index = Ops.add(index, 1) next # start of a quoted string elsif character == "\"" state = :in_quoted_string else # start of a string state = :in_string if character == "\\\"" str = "\"" else str = character end end # after double quoted string - handle non-separator chars after double quote elsif state == :in_quoted_string_after_dblqt if Builtins.issubstring(separator, character) == true if unique == true ret = Builtins.add(ret, str) if !Builtins.contains(ret, str) else ret = Builtins.add(ret, str) end str = "" state = :out_of_string elsif character == "\\\"" str = Ops.add(str, "\"") else str = Ops.add(str, character) end elsif state == :in_quoted_string if character == "\"" # end of quoted string state = :in_quoted_string_after_dblqt elsif character == "\\\"" str = Ops.add(str, "\"") else str = Ops.add(str, character) end elsif state == :in_string if Builtins.issubstring(separator, character) == true state = :out_of_string str = CutBlanks(str) if remove_whitespace == true if unique == true ret = Builtins.add(ret, str) if !Builtins.contains(ret, str) else ret = Builtins.add(ret, str) end str = "" elsif character == "\\\"" str = Ops.add(str, "\"") else str = Ops.add(str, character) end end index = Ops.add(index, 1) end # error - still in quoted string if state == :in_quoted_string || state == :in_quoted_string_after_dblqt if state == :in_quoted_string Builtins.y2warning( "Missing trainling double quote character(\") in input: '%1'", ) end if unique == true ret = Builtins.add(ret, str) if !Builtins.contains(ret, str) else ret = Builtins.add(ret, str) end end # process last string in the buffer if state == :in_string str = CutBlanks(str) if remove_whitespace if unique == true ret = Builtins.add(ret, str) if !Builtins.contains(ret, str) else ret = Builtins.add(ret, str) end end end Builtins.y2debug("Parsed values: %1", ret) deep_copy(ret) end |
- (Hash) ParseSysconfigComment(comment)
Parse metadata from a sysconfig comment
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 |
# File '../../src/modules/String.rb', line 973 def ParseSysconfigComment(comment) ret = {} # get metadata part of comment = GetMetaDataLines(comment) joined_multilines = [] multiline = "" Builtins.y2debug("metadata: %1", ) # join multi line metadata lines Builtins.foreach() do || if Builtins.substring( , Ops.subtract(Builtins.size(), 1), 1 ) != "\\" if multiline != "" # this not first multiline so remove comment mark without_comment = Builtins.regexpsub(, "^##(.*)", "\\1") = without_comment if without_comment != nil end joined_multilines = Builtins.add( joined_multilines, Ops.add(multiline, ) ) multiline = "" else part = Builtins.substring( , 0, Ops.subtract(Builtins.size(), 1) ) if multiline != "" # this not first multiline so remove comment mark without_comment = Builtins.regexpsub(part, "^##(.*)", "\\1") part = without_comment if without_comment != nil end # add line to the previous lines multiline = Ops.add(multiline, part) end end Builtins.y2debug( "metadata after multiline joining: %1", joined_multilines ) # parse each metadata line Builtins.foreach(joined_multilines) do || # Ignore lines with ### -- general comments next if Builtins.regexpmatch(, "^###") = Builtins.regexpsub(, "^##[ \t]*(.*)", "\\1") # split sting to the tag and value part colon_pos = Builtins.findfirstof(, ":") tag = "" val = "" if colon_pos == nil # colon is missing tag = else tag = Builtins.substring(, 0, colon_pos) if Ops.greater_than(Builtins.size(), Ops.add(colon_pos, 1)) val = Builtins.substring(, Ops.add(colon_pos, 1)) end end # remove whitespaces from parts tag = CutBlanks(tag) val = CutBlanks(val) Builtins.y2debug("tag: %1 val: '%2'", tag, val) # add tag and value to map if they are present in comment if tag != "" ret = Builtins.add(ret, tag, val) else # ignore separator lines if !Builtins.regexpmatch(, "^#*$") Builtins.y2warning("Unknown metadata line: %1", ) end end end Builtins.y2debug("parsed sysconfig comment: %1", ret) deep_copy(ret) end |
- (Object) Quote(var)
Quote a string with 's
More precisely it protects single quotes inside the string but does not prepend or append single quotes.
63 64 65 66 |
# File '../../src/modules/String.rb', line 63 def Quote(var) return "" if var == nil || var == "" Builtins.mergestring(Builtins.splitstring(var, "'"), "'\\''") end |
- (Object) Random(len)
Make a random base-36 number. srandom should be called beforehand.
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 |
# File '../../src/modules/String.rb', line 1194 def Random(len) return "" if Ops.less_or_equal(len, 0) digits = Ops.add(@cdigit, @clower) # uses the character classes from above base = Builtins.size(digits) max = 1 i = len while Ops.greater_than(i, 0) max = Ops.multiply(max, base) i = Ops.subtract(i, 1) end rnum = Builtins.random(max) ret = "" i = len while Ops.greater_than(i, 0) digit = Ops.modulo(rnum, base) rnum = Ops.divide(rnum, base) ret = Ops.add(ret, Builtins.substring(digits, digit, 1)) i = Ops.subtract(i, 1) end ret end |
- (String) RemoveShortcut(label)
Remove a shortcut from a label, so that it can be inserted into help to avoid risk of different translation of the label
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 |
# File '../../src/modules/String.rb', line 1278 def RemoveShortcut(label) ret = label if Builtins.regexpmatch(label, "^(.*[^&])?(&&)*&[[:alnum:]].*$") ret = Builtins.regexpsub( label, "^((.*[^&])?(&&)*)&([[:alnum:]].*)$", "\\1\\4" ) end ret end |
- (String) Repeat(text, number)
Repeat a string
Repeat a string number of times.
351 352 353 354 355 356 357 358 359 360 361 362 |
# File '../../src/modules/String.rb', line 351 def Repeat(text, number) text = "" if text == nil ret = "" while Ops.greater_than(number, 0) ret = Ops.add(ret, text) number = Ops.subtract(number, 1) end ret end |
- (String) Replace(s, source, target)
Replace substring in a string. All substrings source are replaced by string target.
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
# File '../../src/modules/String.rb', line 1071 def Replace(s, source, target) return nil if s == nil if source == nil || source == "" Builtins.y2warning("invalid parameter source: %1", source) return s end if target == nil Builtins.y2warning("invalid parameter target: %1", target) return s end pos = Builtins.find(s, source) while Ops.greater_or_equal(pos, 0) tmp = Ops.add(Builtins.substring(s, 0, pos), target) if Ops.greater_than( Builtins.size(s), Ops.add(pos, Builtins.size(source)) ) tmp = Ops.add( tmp, Builtins.substring(s, Ops.add(pos, Builtins.size(source))) ) end s = tmp pos = Builtins.find(s, source) end s end |
- (Object) ReplaceWith(str, chars, glue)
Replaces all characters in a given string with some other string or character
1323 1324 1325 |
# File '../../src/modules/String.rb', line 1323 def ReplaceWith(str, chars, glue) Builtins.mergestring(Builtins.splitstring(str, chars), glue) end |
- (Object) StartsWith(str, test)
Checks whether string str starts with test.
1291 1292 1293 |
# File '../../src/modules/String.rb', line 1291 def StartsWith(str, test) Builtins.search(str, test) == 0 end |
- (Object) SuperPad(text, length, padding, alignment)
Add the padding character around the text to make it long enough
Add the padding character around the text to make it long enough. If the text is longer than requested, no changes are made.
375 376 377 378 379 380 381 382 383 384 385 |
# File '../../src/modules/String.rb', line 375 def SuperPad(text, length, padding, alignment) text = "" if text == nil pad = Repeat(padding, Ops.subtract(length, Builtins.size(text))) if alignment == :right return Ops.add(pad, text) else return Ops.add(text, pad) end end |
- (String) TextTable(header, items, options)
Function creates text table without using HTML tags. (Useful for commandline) Undefined option uses the default one.
Header: [ “Id”, “Configuration”, “Device” ] Items: [ [ "1", "aaa", "Samsung Calex" ], [ "2", "bbb", "Trivial Trinitron" ] ] Possible Options: horizontal_padding (for columns), table_left_padding (for table)
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 |
# File '../../src/modules/String.rb', line 861 def TextTable(header, items, ) header = deep_copy(header) items = deep_copy(items) = deep_copy() current_horizontal_padding = Ops.get_integer( , "horizontal_padding", 2 ) current_table_left_padding = Ops.get_integer( , "table_left_padding", 4 ) cols_lenghts = FindLongestRecords(Builtins.add(items, header)) # whole table is left-padded table_left_padding = Pad("", current_table_left_padding) # the last row has no newline rows_count = Builtins.size(items) table = "" table = Ops.add( Ops.add( Ops.add(table, table_left_padding), CreateTableRow(header, cols_lenghts, current_horizontal_padding) ), "\n" ) table = Ops.add( Ops.add( Ops.add(table, table_left_padding), CreateTableHeaderUnderline(cols_lenghts, current_horizontal_padding) ), "\n" ) rows_counter = 1 Builtins.foreach(items) do |row| table = Ops.add( Ops.add( Ops.add(table, table_left_padding), CreateTableRow(row, cols_lenghts, current_horizontal_padding) ), Ops.less_than(rows_counter, rows_count) ? "\n" : "" ) rows_counter = Ops.add(rows_counter, 1) end table end |
- (String) UnderlinedHeader(header_line, left_padding)
Function returns underlined text header without using HTML tags. (Useful for commandline)
918 919 920 921 922 923 924 925 926 |
# File '../../src/modules/String.rb', line 918 def UnderlinedHeader(header_line, left_padding) Ops.add( Ops.add( Ops.add(Ops.add(Pad("", left_padding), header_line), "\n"), Pad("", left_padding) ), CreateUnderline(Builtins.size(header_line)) ) end |
- (Object) UnQuote(var)
Unquote a string with 's (quoted with quote)
72 73 74 75 76 77 78 79 80 |
# File '../../src/modules/String.rb', line 72 def UnQuote(var) return "" if var == nil || var == "" Builtins.y2debug("var=%1", var) while Builtins.regexpmatch(var, "'\\\\''") var = Builtins.regexpsub(var, "(.*)'\\\\''(.*)", "\\1'\\2") Builtins.y2debug("var=%1", var) end var end |
- (String) ValidCharsFilename
Characters valid in a filename (not pathname). Naturally "/" is disallowed. Otherwise, the graphical ASCII characters are allowed.
744 745 746 |
# File '../../src/modules/String.rb', line 744 def ValidCharsFilename Builtins.deletechars(CGraph(), "/") end |
- (String) WrapAt(text, width, split_string)
Returns text wrapped at defined margin. Very useful for translated strings used for pop-up windows or dialogs where you can't know the width. It controls the maximum width of the string so the text should allways fit into the minimal ncurses window. If you expect some long words, such us URLs or words with a hyphen inside, you can also set the additional split-characters to "/-". Then the function can wrap the word also after these characters. This function description was wrapped using the function String::WrapAt().
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
# File '../../src/modules/String.rb', line 1119 def WrapAt(text, width, split_string) new_string = "" avail = width # characters available in this line lsep = "" # set to "\n" when at the beginning of a new line wsep = "" # set to " " after words, unless at the beginning Builtins.foreach(Builtins.splitstring(text, " \n")) do |word| while Ops.greater_than(Builtins.size(word), 0) # decide where to split the current word split_at = 0 if Ops.less_or_equal(Builtins.size(word), width) split_at = Builtins.size(word) else split_at = Builtins.findlastof( Builtins.substring( word, 0, Ops.subtract(avail, Builtins.size(wsep)) ), Ops.add(" \n", split_string) ) if split_at != nil split_at = Ops.add(split_at, 1) else split_at = Builtins.findlastof( Builtins.substring(word, 0, width), Ops.add(" \n", split_string) ) if split_at != nil split_at = Ops.add(split_at, 1) else split_at = Ops.subtract(avail, Builtins.size(wsep)) end end end # decide whether it fits into the same line or must go on # a separate line if Ops.greater_than(Ops.add(Builtins.size(wsep), split_at), avail) if Ops.greater_than(Builtins.size(new_string), 0) new_string = Ops.add(new_string, "\n") end avail = width wsep = "" lsep = "" end # add the next word or partial word new_string = Ops.add( Ops.add(Ops.add(new_string, lsep), wsep), Builtins.substring(word, 0, split_at) ) avail = Ops.subtract( Ops.subtract(avail, Builtins.size(wsep)), split_at ) wsep = "" lsep = "" if avail == 0 avail = width lsep = "\n" elsif split_at == Builtins.size(word) wsep = " " end word = Builtins.substring(word, split_at) end end new_string end |
- (Boolean) YesNo(value)
Returns value as “Yes” or “No”
110 111 112 113 114 115 116 117 118 |
# File '../../src/modules/String.rb', line 110 def YesNo(value) if value # human text for Boolean value return _("Yes") else # human text for Boolean value return _("No") end end |