PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/perl/Choicetool/OS/String.pm

#
Perl | 172 lines | 107 code | 42 blank | 23 comment | 4 complexity | 17e1271b510cdc11034078a7aa306dfe MD5 | raw file
Possible License(s): GPL-2.0
  1. # -*- perl -*-
  2. #
  3. # String.pm
  4. #
  5. # Copyright (C) 2008, 2009 Francesco Salvestrini
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. package Choicetool::OS::String;
  22. use 5.8.0;
  23. use warnings;
  24. use strict;
  25. use diagnostics;
  26. use Choicetool::Base::Debug;
  27. use Choicetool::Base::Trace;
  28. BEGIN {
  29. use Exporter ();
  30. our ($VERSION, @ISA, @EXPORT);
  31. @ISA = qw(Exporter);
  32. @EXPORT = qw(&string_tofile
  33. &string_uppercase
  34. &string_lowercase
  35. &string_purify
  36. &string_replace
  37. &string_replace_many
  38. &string_isnumber
  39. &string_tonumber);
  40. }
  41. sub string_isnumber ($)
  42. {
  43. my $string = shift;
  44. assert(defined($string));
  45. if ($string == "$string") {
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. sub string_tonumber ($)
  51. {
  52. my $string = shift;
  53. assert(defined($string));
  54. my $t;
  55. foreach my $d (split(//, shift())) {
  56. $t = $t * 10 + $d;
  57. }
  58. return $t;
  59. }
  60. sub string_replace ($$$)
  61. {
  62. my $string = shift;
  63. my $from = shift;
  64. my $to = shift;
  65. assert(defined($string));
  66. assert(defined($from));
  67. assert(defined($to));
  68. $string =~ s/$from/$to/g;
  69. return $string;
  70. }
  71. sub string_replace_many ($$)
  72. {
  73. my $string = shift;
  74. my $hash_ref = shift;
  75. assert(defined($string));
  76. assert(defined($hash_ref));
  77. my %hash;
  78. %hash = %{$hash_ref};
  79. for my $key (keys(%hash)) {
  80. my $value;
  81. $value = $hash{$key};
  82. assert(defined($value));
  83. $string = string_replace($string, $key, $value);
  84. }
  85. return $string;
  86. }
  87. sub string_uppercase ($)
  88. {
  89. my $string = shift;
  90. assert(defined($string));
  91. return uc($string);
  92. }
  93. sub string_lowercase ($)
  94. {
  95. my $string = shift;
  96. assert(defined($string));
  97. return lc($string);
  98. }
  99. #
  100. # Removes duplicated spaces and tabs from input string
  101. #
  102. sub string_purify ($)
  103. {
  104. my $string = shift;
  105. assert(defined($string));
  106. chomp $string;
  107. $string =~ s/\t+/\ /;
  108. $string =~ s/\s+/\ /;
  109. assert(defined($string));
  110. return $string;
  111. }
  112. sub string_tofile ($$)
  113. {
  114. my $string = shift;
  115. my $filename = shift;
  116. assert(defined($filename));
  117. my $filehandle;
  118. if (!open($filehandle, ">", $filename)) {
  119. error("Cannot open \`$filename' for output");
  120. return 0;
  121. }
  122. print $filehandle $string;
  123. close($filehandle);
  124. return 1;
  125. }
  126. 1;