2006-11-27 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Relaxng / Misc.cs
1 //
2 // Commons.Xml.Relaxng.General.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // 2003 Atsushi Enomoto "No rights reserved."
8 //
9 // Copyright (c) 2004 Novell Inc.
10 // All rights reserved
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 using System;
34 using System.Collections;
35 using System.IO;
36 using System.Text;
37 using System.Xml;
38 using Commons.Xml.Relaxng.Derivative;
39
40 namespace Commons.Xml.Relaxng
41 {
42         internal class Util
43         {
44                 public static string NormalizeWhitespace (string s)
45                 {
46                         if (s.Length == 0)
47                                 return s;
48
49                         StringBuilder sb = null;
50                         int noSpaceIndex = 0;
51                         bool inSpace = false;
52
53                         for (int i = 0; i < s.Length; i++) {
54                                 switch (s [i]) {
55                                 case ' ':
56                                 case '\r':
57                                 case '\t':
58                                 case '\n':
59                                         if (inSpace)
60                                                 continue;
61                                         if (sb == null)
62                                                 sb = new StringBuilder (s.Length);
63                                         if (noSpaceIndex < i) {
64                                                 if (sb.Length > 0)
65                                                         sb.Append (' ');
66                                                 sb.Append (s, noSpaceIndex, i - noSpaceIndex);
67                                         }
68                                         inSpace = true;
69                                         break;
70                                 default:
71                                         if (inSpace) {
72                                                 noSpaceIndex = i;
73                                                 inSpace = false;
74                                         }
75                                         break;
76                                 }
77                         }
78                         if (sb == null)
79                                 return s;
80                         if (!inSpace && noSpaceIndex < s.Length) {
81                                 sb.Append (' ');
82                                 sb.Append (s, noSpaceIndex, s.Length - noSpaceIndex);
83                         }
84                         return sb.ToString ();
85                 }
86
87                 public static bool IsWhitespace (string s)
88                 {
89                         for (int i = 0; i < s.Length; i++) {
90                                 switch (s [i]) {
91                                 case ' ':
92                                 case '\t':
93                                 case '\n':
94                                 case '\r':
95                                         continue;
96                                 default:
97                                         return false;
98                                 }
99                         }
100                         return true;
101                 }
102         }
103 }
104