1 #region License
2 // Copyright (c) 2007 James Newton-King
3 //
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 #endregion
25 
26 using System;
27 
28 namespace Newtonsoft.Json.Utilities
29 {
30     internal struct StringReference
31     {
32         private readonly char[] _chars;
33         private readonly int _startIndex;
34         private readonly int _length;
35 
36         public char this[int i]
37         {
38             get { return _chars[i]; }
39         }
40 
41         public char[] Chars
42         {
43             get { return _chars; }
44         }
45 
46         public int StartIndex
47         {
48             get { return _startIndex; }
49         }
50 
51         public int Length
52         {
53             get { return _length; }
54         }
55 
StringReferenceNewtonsoft.Json.Utilities.StringReference56         public StringReference(char[] chars, int startIndex, int length)
57         {
58             _chars = chars;
59             _startIndex = startIndex;
60             _length = length;
61         }
62 
ToStringNewtonsoft.Json.Utilities.StringReference63         public override string ToString()
64         {
65             return new string(_chars, _startIndex, _length);
66         }
67     }
68 
69     internal static class StringReferenceExtensions
70     {
IndexOf(this StringReference s, char c, int startIndex, int length)71         public static int IndexOf(this StringReference s, char c, int startIndex, int length)
72         {
73             int index = Array.IndexOf(s.Chars, c, s.StartIndex + startIndex, length);
74             if (index == -1)
75             {
76                 return -1;
77             }
78 
79             return index - s.StartIndex;
80         }
81 
StartsWith(this StringReference s, string text)82         public static bool StartsWith(this StringReference s, string text)
83         {
84             if (text.Length > s.Length)
85             {
86                 return false;
87             }
88 
89             char[] chars = s.Chars;
90 
91             for (int i = 0; i < text.Length; i++)
92             {
93                 if (text[i] != chars[i + s.StartIndex])
94                 {
95                     return false;
96                 }
97             }
98 
99             return true;
100         }
101 
EndsWith(this StringReference s, string text)102         public static bool EndsWith(this StringReference s, string text)
103         {
104             if (text.Length > s.Length)
105             {
106                 return false;
107             }
108 
109             char[] chars = s.Chars;
110 
111             int start = s.StartIndex + s.Length - text.Length;
112             for (int i = 0; i < text.Length; i++)
113             {
114                 if (text[i] != chars[i + start])
115                 {
116                     return false;
117                 }
118             }
119 
120             return true;
121         }
122     }
123 }