~bratsche/ubuntu/maverick/monodevelop/disable-appmenu

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Gettext/MonoDevelop.Gettext/StringEscaping.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-03-29 23:36:33 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080329233633-wq5p1rktg8ek3vxc
Tags: 1.0+dfsg-1ubuntu1
* Merge from Debian unstable. (LP: #209012) Remaining Ubuntu changes:
+ debian/control: 
  - Build-dep on xulrunner-1.9-dev instead of firefox-dev
  - Depend on xulrunner-1.9 instead of firefox
+ don't do any MOZILLA_HOME/MOZILLA_FIVE_HOME business which isn't needed
  for xulrunner-1.9 using the standalone glue anymore (gecko gil uses
  standalone by default)
  - add debian/patches/use_xulrunner_1.9.dpatch
  - update debian/patches/00list
* Remove build-dep on libxul-dev and the dep on libxul0d
  since we are using xulrunner1.9 instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// StringEscaping.cs
 
3
// 
 
4
// Author:
 
5
//   Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Globalization;
 
31
using System.Text;
 
32
 
 
33
namespace MonoDevelop.Gettext
 
34
{
 
35
        
 
36
        
 
37
        public static class StringEscaping
 
38
        {
 
39
                //based on http://www-ccs.ucsd.edu/c/charset.html
 
40
                //with modififications, as Gettext dosn't follow all C escaping
 
41
                public static string ToGettextFormat (string text)
 
42
                {
 
43
                        StringBuilder sb = new StringBuilder ();
 
44
                        for (int i = 0; i < text.Length; i++) {
 
45
                                char c = text[i];
 
46
                                switch (c) {
 
47
                                case '"':
 
48
                                        sb.Append ("\\\"");
 
49
                                        continue;
 
50
                                
 
51
                                //Gettext doesn't like escaping this
 
52
                                //case '\'':
 
53
                                //      sb.Append ("\\'");
 
54
                                //      continue;
 
55
                                
 
56
                                //These shouldn't be in translations... but will be caught by IsControl
 
57
                                //case '\a':
 
58
                                //case '\b':
 
59
                                //case '\f':
 
60
                                //case '\v':
 
61
                                        
 
62
                                //this doesn't matter since we're not dealing with trigraphs
 
63
                                //case "?":
 
64
                                //      sb.Append ("\\?");
 
65
                                //      continue;
 
66
                                        
 
67
                                case '\\':
 
68
                                        sb.Append ("\\\\");
 
69
                                        continue;
 
70
                                case '\n':
 
71
                                        sb.Append ("\\n");
 
72
                                        continue;
 
73
                                case '\r':
 
74
                                        sb.Append ("\\r");
 
75
                                        continue;
 
76
                                case '\t':
 
77
                                        sb.Append ("\\t");
 
78
                                        continue;
 
79
                                }
 
80
                                
 
81
                                if (char.IsControl (c))
 
82
                                        throw new FormatException ("Invalid character in translatable string");
 
83
                                
 
84
                                sb.Append (c);
 
85
                        }
 
86
                        
 
87
                        return sb.ToString ();
 
88
                }
 
89
                
 
90
                public static string UnEscape (EscapeMode mode, string text)
 
91
                {
 
92
                        switch (mode) {
 
93
                        case EscapeMode.None:
 
94
                                return text;
 
95
                        case EscapeMode.CSharp:
 
96
                                return FromCSharpFormat (text);
 
97
                        case EscapeMode.CSharpVerbatim:
 
98
                                return FromCSharpVerbatimFormat (text);
 
99
                        case EscapeMode.Xml:
 
100
                                return FromXml (text);
 
101
                        default:
 
102
                                throw new Exception ("Unknown string escaping mode '" + mode.ToString () + "'");
 
103
                        }
 
104
                }
 
105
                
 
106
                /*
 
107
                //based on http://www-ccs.ucsd.edu/c/charset.html 
 
108
                public static string FromCFormat (string text)
 
109
                {
 
110
                }
 
111
                */
 
112
                
 
113
                //based on the C# 2.0 spec
 
114
                static string FromCSharpVerbatimFormat (string text)
 
115
                {
 
116
                        StringBuilder sb = new StringBuilder ();
 
117
                        for (int i = 0; i < text.Length; i++) {
 
118
                                char c1 = text[i];
 
119
                                if (c1 == '"') {
 
120
                                        i++;
 
121
                                        char c2 = text[i];
 
122
                                        if (c2 != '"')
 
123
                                                throw new FormatException ("Unescaped '\"' character in C# verbatim string.");
 
124
                                }
 
125
                                sb.Append (c1);
 
126
                        }
 
127
                        return sb.ToString ();  
 
128
                }
 
129
                
 
130
                static string FromXml (string text)
 
131
                {
 
132
                        StringBuilder sb = new StringBuilder ();
 
133
                        for (int i = 0; i < text.Length; i++) {
 
134
                                char c1 = text[i];
 
135
                                if (c1 == '&') {
 
136
                                        int end = text.IndexOf (';', i);
 
137
                                        if (end == -1)
 
138
                                                throw new FormatException ("Unterminated XML entity.");
 
139
                                        string entity = text.Substring (i+1, end - i - 1);
 
140
                                        switch (entity) {
 
141
                                        case "lt":
 
142
                                                sb.Append ('<');
 
143
                                                break;
 
144
                                        case "gt":
 
145
                                                sb.Append ('>');
 
146
                                                break;
 
147
                                        case "amp":
 
148
                                                sb.Append ('&');
 
149
                                                break;
 
150
                                        case "apos":
 
151
                                                sb.Append ('\'');
 
152
                                                break;
 
153
                                        case "quot":
 
154
                                                sb.Append ('"');
 
155
                                                break;
 
156
                                        default:
 
157
                                                throw new FormatException ("Unrecogised XML entity '&" + entity + ";'.");
 
158
                                        }
 
159
                                        i = end;
 
160
                                } else
 
161
                                        sb.Append (c1);
 
162
                        }
 
163
                        return sb.ToString ();  
 
164
                }
 
165
                
 
166
                //based on the C# 2.0 spec
 
167
                static string FromCSharpFormat (string text)
 
168
                {
 
169
                        StringBuilder sb = new StringBuilder ();
 
170
                        for (int i = 0; i < text.Length; i++) {
 
171
                                char c1 = text[i];
 
172
                                if (c1 != '\\') {
 
173
                                        sb.Append (c1);
 
174
                                        continue;
 
175
                                }
 
176
                                
 
177
                                i++;
 
178
                                char c2 = text[i];
 
179
                                
 
180
                                switch (c2) {
 
181
                                case '\'':
 
182
                                case '"':
 
183
                                case '\\':
 
184
                                        sb.Append (c2);
 
185
                                        break;
 
186
                                case 'n':
 
187
                                        sb.Append ('\n');
 
188
                                        break;
 
189
                                case 'r':
 
190
                                        sb.Append ('\r');
 
191
                                        break;
 
192
                                case 't':
 
193
                                        sb.Append ('\t');
 
194
                                        break;
 
195
                                case 'U':
 
196
                                        //FIXME UNICODE
 
197
                                        //break;
 
198
                                case 'u':
 
199
                                        //FIXME unicode
 
200
                                        //break;
 
201
                                case 'x':
 
202
                                        //FIXME hex unicode
 
203
                                        //break;
 
204
                                        //if (char.IsControl (c);
 
205
                                        
 
206
                                //case '0':
 
207
                                //case 'a':
 
208
                                //case 'b':
 
209
                                //case 'f':
 
210
                                //case 'v':
 
211
                                default:
 
212
                                        throw new FormatException ("Invalid escape '\\" + c2 + "' in translatable string.");
 
213
                                }
 
214
                                
 
215
                        }
 
216
                        return sb.ToString ();
 
217
                }
 
218
                
 
219
                public enum EscapeMode
 
220
                {
 
221
                        None,
 
222
                        CSharp,
 
223
                        CSharpVerbatim,
 
224
                        Xml
 
225
                }
 
226
        }
 
227
}