Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / xsp / System / Web / Extensions / Script / Serialization / JavaScriptString.cs / 1 / JavaScriptString.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Script.Serialization { using System; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text; internal class JavaScriptString { private string _s; private int _index; internal JavaScriptString(string s) { _s = s; } internal NullableGetNextNonEmptyChar() { while (_s.Length > _index) { char c = _s[_index++]; if (!Char.IsWhiteSpace(c)) { return c; } } return null; } internal Nullable MoveNext() { if (_s.Length > _index) { return _s[_index++]; } return null; } internal string MoveNext(int count) { if (_s.Length >= _index + count) { string result = _s.Substring(_index, count); _index += count; return result; } return null; } internal void MovePrev() { if (_index > 0) { _index--; } } internal void MovePrev(int count) { while (_index > 0 && count > 0) { _index--; count--; } } private static void AppendCharAsUnicode(StringBuilder builder, char c) { builder.Append("\\u"); builder.AppendFormat(CultureInfo.InvariantCulture, "{0:x4}", (int)c); } [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification="Method would be more complex if split into separate pieces.")] internal static string QuoteString(string value) { StringBuilder b = null; if (String.IsNullOrEmpty(value)) { return String.Empty; } int startIndex = 0; int count = 0; for (int i = 0; i < value.Length; i++) { char c = value[i]; // Append the unhandled characters (that do not require special treament) // to the string builder when special characters are detected. if (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' || c == '\\' || c == '\n' || c == '\b' || c == '\f' || c < ' ') { if (b == null) { b = new StringBuilder(value.Length + 5); } if (count > 0) { b.Append(value, startIndex, count); } startIndex = i+1; count = 0; } switch (c) { case '\r': b.Append("\\r"); break; case '\t': b.Append("\\t"); break; case '\"': b.Append("\\\""); break; case '\\': b.Append("\\\\"); break; case '\n': b.Append("\\n"); break; case '\b': b.Append("\\b"); break; case '\f': b.Append("\\f"); break; case '\'': case '>': case '<': AppendCharAsUnicode(b, c); break; default: if (c < ' ') { AppendCharAsUnicode(b, c); } else { count++; } break; } } if (b == null) { return value; } if (count > 0) { b.Append(value, startIndex, count); } return b.ToString(); } internal static string QuoteString(string value, bool addQuotes) { string s = QuoteString(value); if (addQuotes) { s = "\"" + s + "\""; } return s; } public override string ToString() { if (_s.Length > _index) { return _s.Substring(_index); } return String.Empty; } internal string GetDebugString(string message) { return message + " (" + _index + "): " + _s; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Script.Serialization { using System; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text; internal class JavaScriptString { private string _s; private int _index; internal JavaScriptString(string s) { _s = s; } internal NullableGetNextNonEmptyChar() { while (_s.Length > _index) { char c = _s[_index++]; if (!Char.IsWhiteSpace(c)) { return c; } } return null; } internal Nullable MoveNext() { if (_s.Length > _index) { return _s[_index++]; } return null; } internal string MoveNext(int count) { if (_s.Length >= _index + count) { string result = _s.Substring(_index, count); _index += count; return result; } return null; } internal void MovePrev() { if (_index > 0) { _index--; } } internal void MovePrev(int count) { while (_index > 0 && count > 0) { _index--; count--; } } private static void AppendCharAsUnicode(StringBuilder builder, char c) { builder.Append("\\u"); builder.AppendFormat(CultureInfo.InvariantCulture, "{0:x4}", (int)c); } [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification="Method would be more complex if split into separate pieces.")] internal static string QuoteString(string value) { StringBuilder b = null; if (String.IsNullOrEmpty(value)) { return String.Empty; } int startIndex = 0; int count = 0; for (int i = 0; i < value.Length; i++) { char c = value[i]; // Append the unhandled characters (that do not require special treament) // to the string builder when special characters are detected. if (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' || c == '\\' || c == '\n' || c == '\b' || c == '\f' || c < ' ') { if (b == null) { b = new StringBuilder(value.Length + 5); } if (count > 0) { b.Append(value, startIndex, count); } startIndex = i+1; count = 0; } switch (c) { case '\r': b.Append("\\r"); break; case '\t': b.Append("\\t"); break; case '\"': b.Append("\\\""); break; case '\\': b.Append("\\\\"); break; case '\n': b.Append("\\n"); break; case '\b': b.Append("\\b"); break; case '\f': b.Append("\\f"); break; case '\'': case '>': case '<': AppendCharAsUnicode(b, c); break; default: if (c < ' ') { AppendCharAsUnicode(b, c); } else { count++; } break; } } if (b == null) { return value; } if (count > 0) { b.Append(value, startIndex, count); } return b.ToString(); } internal static string QuoteString(string value, bool addQuotes) { string s = QuoteString(value); if (addQuotes) { s = "\"" + s + "\""; } return s; } public override string ToString() { if (_s.Length > _index) { return _s.Substring(_index); } return String.Empty; } internal string GetDebugString(string message) { return message + " (" + _index + "): " + _s; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SortableBindingList.cs
- SystemResources.cs
- HwndHostAutomationPeer.cs
- NotImplementedException.cs
- PtsHost.cs
- DataServiceCollectionOfT.cs
- ColorContext.cs
- ScrollBarRenderer.cs
- RadialGradientBrush.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- NotificationContext.cs
- XMLDiffLoader.cs
- SemaphoreSecurity.cs
- SoapIncludeAttribute.cs
- DragDeltaEventArgs.cs
- EventManager.cs
- OdbcStatementHandle.cs
- InkCanvasSelectionAdorner.cs
- SubpageParagraph.cs
- ClientBuildManager.cs
- Rule.cs
- CalendarKeyboardHelper.cs
- storagemappingitemcollection.viewdictionary.cs
- OdbcCommand.cs
- CompositeKey.cs
- WindowsContainer.cs
- RTLAwareMessageBox.cs
- WebPartDisplayModeCollection.cs
- AspNetSynchronizationContext.cs
- _UriSyntax.cs
- DummyDataSource.cs
- DynamicMethod.cs
- FollowerQueueCreator.cs
- ListViewGroup.cs
- AnimationClockResource.cs
- DirectoryInfo.cs
- ClientSettingsSection.cs
- PropertyFilterAttribute.cs
- InternalCache.cs
- DependencyObject.cs
- RemoteWebConfigurationHost.cs
- securitymgrsite.cs
- TextRange.cs
- LeaseManager.cs
- AppSecurityManager.cs
- NotifyParentPropertyAttribute.cs
- GeneratedContractType.cs
- FrameworkElementFactoryMarkupObject.cs
- ReflectionPermission.cs
- PageThemeParser.cs
- DataSetMappper.cs
- StaticFileHandler.cs
- QilChoice.cs
- QueryStringParameter.cs
- ResXResourceSet.cs
- PtsHost.cs
- XmlRawWriterWrapper.cs
- TargetControlTypeAttribute.cs
- AssemblyNameProxy.cs
- BitmapVisualManager.cs
- METAHEADER.cs
- ContextInformation.cs
- AdapterUtil.cs
- EncoderReplacementFallback.cs
- WinEventHandler.cs
- MissingFieldException.cs
- UIInitializationException.cs
- ContextMenuService.cs
- Trace.cs
- LoadGrammarCompletedEventArgs.cs
- WebPartConnectVerb.cs
- AssemblyBuilder.cs
- HierarchicalDataBoundControl.cs
- DelegateArgumentReference.cs
- DNS.cs
- ActivityXRefPropertyEditor.cs
- SchemaInfo.cs
- WorkerProcess.cs
- DataConnectionHelper.cs
- Maps.cs
- XpsFilter.cs
- Stylesheet.cs
- DSASignatureDeformatter.cs
- HierarchicalDataTemplate.cs
- XmlExpressionDumper.cs
- OdbcInfoMessageEvent.cs
- UrlMappingsSection.cs
- RegisteredExpandoAttribute.cs
- ResourceKey.cs
- WebConfigurationFileMap.cs
- IsolatedStorageFilePermission.cs
- XmlSignificantWhitespace.cs
- SystemIPGlobalStatistics.cs
- ItemsControl.cs
- UInt16.cs
- DataGridAutoGeneratingColumnEventArgs.cs
- WriteTimeStream.cs
- TagPrefixCollection.cs
- Stroke2.cs
- GridViewRow.cs