ni_nitdoc: added fast copy past utility to signatures.
[nit.git] / src / utils.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Various functions for NIT tools
19 package utils
20
21 import symbol
22
23 # Mangle an array of symbol using only alphanums and underscores
24 fun cmangle(symbols: Symbol...): String
25 do
26 var table = once cmangle_table
27 var res = new Buffer
28 for sym in symbols do
29 if not res.is_empty then
30 res.add('_')
31 res.add('_')
32 res.add('_')
33 end
34 var underscore = false
35 var normal = true
36 var s = sym.to_s
37 for c in s do
38 if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') or (c >= '0' and c <= '9') then
39 res.add(c)
40 underscore = false
41 normal = true
42 else if c == '_' and not underscore then
43 res.add(c)
44 underscore = true
45 normal = true
46 else if table.has_key(c) then
47 if normal then
48 res.add('_')
49 res.add('_')
50 end
51 res.append(table[c])
52 normal = false
53 underscore = false
54 end
55 end
56 end
57 return res.to_s
58 end
59
60 # Build the table that associates character to mangle to string
61 private fun cmangle_table: HashMap[Char, String]
62 do
63 var res = new HashMap[Char, String]
64 res['+'] = "plus"
65 res['-'] = "minus"
66 res['*'] = "star"
67 res['/'] = "slash"
68 res['%'] = "percent"
69 res['['] = "bra"
70 res['='] = "eq"
71 res['<'] = "l"
72 res['>'] = "g"
73 res['!'] = "n"
74 res['_'] = "u"
75 res['@'] = "at"
76 return res
77 end
78