File: String.pm

package info (click to toggle)
icheck 0.9.7-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,352 kB
  • sloc: perl: 12,152; makefile: 202; ansic: 100
file content (133 lines) | stat: -rw-r--r-- 2,651 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
package CParse::String;

use 5.6.0;
use strict;
use warnings;

use Exporter;

our @ISA = qw/Exporter/;
our @EXPORT_OK = qw/escape unescape/;

sub escape
  {
    my $str = shift;
    $str =~ s/\\/\\\\/g;
    $str =~ s/\'/\\'/g;
    $str =~ s/\"/\\"/g;
    $str =~ s/\?/\\?/g;
    $str =~ s/\a/\\a/g;
    $str =~ s/\0x08/\\b/g;
    $str =~ s/\f/\\f/g;
    $str =~ s/\n/\\n/g;
    $str =~ s/\r/\\r/g;
    $str =~ s/\t/\\t/g;
    $str =~ s/\0x0b/\\v/g;
    $str =~ s/([^[:print:]])/sprintf "\\x%x", ord($1)/ge;
    return $str;
  }

sub unescape
  {
    my $str = shift;
    my $out = "";

    while (length $str)
      {
        my $c = substr($str, 0, 1, '');
        if ($c eq '\\')
          {
            $c = substr($str, 0, 1, '');
            unless (defined $c)
              {
                die "Bad escape sequence\n";
              }
            if ($c eq '\\')
              {
                $out .= '\\';
              }
            elsif ($c eq '\'')
              {
                $out .= '\'';
              }
            elsif ($c eq '"')
              {
                $out .= '"';
              }
            elsif ($c eq '?')
              {
                $out .= '?';
              }
            elsif ($c eq 'a')
              {
                $out .= "\a";
              }
            elsif ($c eq 'b')
              {
                $out .= "\0x08";
              }
            elsif ($c eq 'f')
              {
                $out .= "\f";
              }
            elsif ($c eq 'n')
              {
                $out .= "\n";
              }
            elsif ($c eq 'r')
              {
                $out .= "\r";
              }
            elsif ($c eq 't')
              {
                $out .= "\t";
              }
            elsif ($c eq 'v')
              {
                $out .= "\0x0b";
              }
            elsif ($c eq 'x')
              {
                $str =~ s/^([[:xdigit:]]+)//;
                $out .= chr(hex($1));
              }
            elsif ($c =~ /^[0-7]$/)
              {
                $str =~ s/^([0-7]{0,2})//;
                $out .= chr(oct($c . $1));
              }
          }
        else
          {
            $out .= $c;
          }
      }

    return $out;
  }

sub new
  {
    my $this = shift;
    my $class = ref($this) || $this;

    my $str = shift;
    my $wide = shift;

    my $self = {value => $str,
                wide => $wide,
               };
    bless $self, $class;
    return $self;
  }

sub dump_c
  {
    my $self = shift;

    my $value = escape($self->{value});

    return $self->{wide} ? "L\"$value\"" : "\"$value\"";
  }

1;