|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 |
|
21 namespace SymbianUtils.Strings |
|
22 { |
|
23 public static class StringParsingUtils |
|
24 { |
|
25 public static void SkipToEndOfSection( ref string aText, ref int aStartPos, char aOpeningChar, char aClosingChar ) |
|
26 { |
|
27 bool found = false; |
|
28 int charCount = 1; |
|
29 int pos = aStartPos; |
|
30 // |
|
31 while ( charCount > 0 && ++pos < aText.Length ) |
|
32 { |
|
33 System.Diagnostics.Debug.Assert( pos >= 0 && pos < aText.Length ); |
|
34 |
|
35 char character = aText[ pos ]; |
|
36 if ( character == aOpeningChar ) |
|
37 ++charCount; |
|
38 else if ( character == aClosingChar ) |
|
39 --charCount; |
|
40 if ( charCount == 0 ) |
|
41 { |
|
42 found = true; |
|
43 break; |
|
44 } |
|
45 } |
|
46 // |
|
47 aStartPos = -1; |
|
48 if ( found == true ) |
|
49 aStartPos = pos; |
|
50 } |
|
51 |
|
52 public static void SkipToBeginningOfSection( ref string aText, ref int aStartPos, char aOpeningChar, char aClosingChar ) |
|
53 { |
|
54 bool found = false; |
|
55 int charCount = 1; |
|
56 int pos = aStartPos; |
|
57 // |
|
58 while ( charCount > 0 && --pos < aText.Length ) |
|
59 { |
|
60 System.Diagnostics.Debug.Assert( pos >= 0 && pos < aText.Length ); |
|
61 |
|
62 char character = aText[ pos ]; |
|
63 if ( character == aClosingChar ) |
|
64 ++charCount; |
|
65 else if ( character == aOpeningChar ) |
|
66 --charCount; |
|
67 if ( charCount == 0 ) |
|
68 { |
|
69 found = true; |
|
70 break; |
|
71 } |
|
72 } |
|
73 // |
|
74 aStartPos = -1; |
|
75 if ( found == true ) |
|
76 aStartPos = pos; |
|
77 } |
|
78 |
|
79 public static bool IsNumeric( string aText, bool aAllowHex ) |
|
80 { |
|
81 bool ret = true; |
|
82 // |
|
83 foreach ( char c in aText ) |
|
84 { |
|
85 if ( char.IsDigit( c ) ) |
|
86 { |
|
87 } |
|
88 else if ( char.IsLetter( c ) && aAllowHex ) |
|
89 { |
|
90 char upper = Char.ToUpper( c ); |
|
91 const string KHexChars = "ABCDEF"; |
|
92 if ( KHexChars.IndexOf( upper ) < 0 ) |
|
93 { |
|
94 ret = false; |
|
95 break; |
|
96 } |
|
97 } |
|
98 else |
|
99 { |
|
100 ret = false; |
|
101 break; |
|
102 } |
|
103 } |
|
104 // |
|
105 return ret; |
|
106 } |
|
107 |
|
108 public static string BytesToString( byte[] aBytes ) |
|
109 { |
|
110 return BytesToString( aBytes, aBytes.Length ); |
|
111 } |
|
112 |
|
113 public static string BytesToString( byte[] aBytes, int aLength ) |
|
114 { |
|
115 return BytesToString( aBytes, 0, aLength ); |
|
116 } |
|
117 |
|
118 public static string BytesToString( byte[] aBytes, int aStart, int aEnd ) |
|
119 { |
|
120 StringBuilder ret = new StringBuilder(); |
|
121 for ( int i = aStart; i < aEnd; i++ ) |
|
122 { |
|
123 byte b = aBytes[ i ]; |
|
124 ret.Append( System.Convert.ToChar( b ) ); |
|
125 } |
|
126 return ret.ToString(); |
|
127 } |
|
128 } |
|
129 } |