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
134
135
136
137
138
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
204
205
206
207
208
209
210
211
212
|
###############################################################################
#
# Package: NaturalDocs::SymbolString
#
###############################################################################
#
# A package to manage <SymbolString> handling throughout the program.
#
###############################################################################
# This file is part of Natural Docs, which is Copyright (C) 2003-2008 Greg Valure
# Natural Docs is licensed under the GPL
use strict;
use integer;
package NaturalDocs::SymbolString;
#
# Function: FromText
#
# Extracts and returns a <SymbolString> from plain text.
#
# This should be the only way to get a <SymbolString> from plain text, as the splitting and normalization must be consistent
# throughout the application.
#
sub FromText #(string textSymbol)
{
my ($self, $textSymbol) = @_;
# The internal format of a symbol is all the normalized identifiers separated by 0x1F characters.
# Convert whitespace and reserved characters to spaces, and condense multiple consecutive ones.
$textSymbol =~ tr/ \t\r\n\x1C\x1D\x1E\x1F/ /s;
# DEPENDENCY: ReferenceString->MakeFrom() assumes all 0x1E characters were removed.
# DEPENDENCY: ReferenceString->MakeFrom() assumes this encoding doesn't use 0x1E characters.
# Remove spaces unless they're separating two alphanumeric/underscore characters.
$textSymbol =~ s/^ //;
$textSymbol =~ s/ $//;
$textSymbol =~ s/(\W) /$1/g;
$textSymbol =~ s/ (\W)/$1/g;
# Remove trailing empty parenthesis, so Function and Function() are equivalent.
$textSymbol =~ s/\(\)$//;
# Split the string into pieces.
my @pieces = split(/(\.|::|->)/, $textSymbol);
my $symbolString;
my $lastWasSeparator = 1;
foreach my $piece (@pieces)
{
if ($piece =~ /^(?:\.|::|->)$/)
{
if (!$lastWasSeparator)
{
$symbolString .= "\x1F";
$lastWasSeparator = 1;
};
}
elsif (length $piece)
{
$symbolString .= $piece;
$lastWasSeparator = 0;
};
# Ignore empty pieces
};
$symbolString =~ s/\x1F$//;
return $symbolString;
};
#
# Function: ToText
#
# Converts a <SymbolString> to text, using the passed separator.
#
sub ToText #(SymbolString symbolString, string separator)
{
my ($self, $symbolString, $separator) = @_;
my @identifiers = $self->IdentifiersOf($symbolString);
return join($separator, @identifiers);
};
#
# Function: ToBinaryFile
#
# Writes a <SymbolString> to the passed filehandle. Can also encode an undef.
#
# Parameters:
#
# fileHandle - The filehandle to write to.
# symbol - The <SymbolString> to write, or undef.
#
# Format:
#
# > [UInt8: number of identifiers]
# > [AString16: identifier] [AString16: identifier] ...
#
# Undef is represented by a zero for the number of identifiers.
#
sub ToBinaryFile #(FileHandle fileHandle, SymbolString symbol)
{
my ($self, $fileHandle, $symbol) = @_;
my @identifiers;
if (defined $symbol)
{ @identifiers = $self->IdentifiersOf($symbol); };
print $fileHandle pack('C', scalar @identifiers);
foreach my $identifier (@identifiers)
{
print $fileHandle pack('nA*', length($identifier), $identifier);
};
};
#
# Function: FromBinaryFile
#
# Loads a <SymbolString> or undef from the filehandle and returns it.
#
# Parameters:
#
# fileHandle - The filehandle to read from.
#
# Returns:
#
# The <SymbolString> or undef.
#
# See also:
#
# See <ToBinaryFile()> for format and dependencies.
#
sub FromBinaryFile #(FileHandle fileHandle)
{
my ($self, $fileHandle) = @_;
my $raw;
# [UInt8: number of identifiers or 0 if none]
read($fileHandle, $raw, 1);
my $identifierCount = unpack('C', $raw);
my @identifiers;
while ($identifierCount)
{
# [AString16: identifier] [AString16: identifier] ...
read($fileHandle, $raw, 2);
my $identifierLength = unpack('n', $raw);
my $identifier;
read($fileHandle, $identifier, $identifierLength);
push @identifiers, $identifier;
$identifierCount--;
};
if (scalar @identifiers)
{ return $self->Join(@identifiers); }
else
{ return undef; };
};
#
# Function: IdentifiersOf
#
# Returns the <SymbolString> as an array of identifiers.
#
sub IdentifiersOf #(SymbolString symbol)
{
my ($self, $symbol) = @_;
return split(/\x1F/, $symbol);
};
#
# Function: Join
#
# Takes a list of identifiers and/or <SymbolStrings> and returns it as a new <SymbolString>.
#
sub Join #(string/SymbolString identifier/symbol, string/SymolString identifier/symbol, ...)
{
my ($self, @pieces) = @_;
# Can't have undefs screwing everything up.
while (scalar @pieces && !defined $pieces[0])
{ shift @pieces; };
# We need to test @pieces first because joining on an empty array returns an empty string rather than undef.
if (scalar @pieces)
{ return join("\x1F", @pieces); }
else
{ return undef; };
};
1;
|