PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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