Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / XmlUtils / System / Xml / Xsl / Runtime / StringConcat.cs / 1 / StringConcat.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
namespace System.Xml.Xsl.Runtime {
///
/// Efficiently concatenates strings when the number of string is not known beforehand, and
/// yet it is common for only one string to be concatenated. StringBuilder is not good for
/// this purpose, since it *always* allocates objects, even if only one string is appended.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public struct StringConcat {
private string s1, s2, s3, s4;
private string delimiter;
private List strList;
int idxStr;
///
/// Clear the result string.
///
public void Clear() {
this.idxStr = 0;
this.delimiter = null;
}
///
/// Gets or sets the string that delimits concatenated strings.
///
public string Delimiter {
get { return this.delimiter; }
set { this.delimiter = value; }
}
///
/// Return the number of concatenated strings, including delimiters.
///
internal int Count {
get { return this.idxStr; }
}
///
/// Concatenate a new string to the result.
///
public void Concat(string value) {
Debug.Assert(value != null);
if (this.delimiter != null && this.idxStr != 0) {
// Add delimiter
ConcatNoDelimiter(this.delimiter);
}
ConcatNoDelimiter(value);
}
///
/// Get the result string.
///
public string GetResult() {
switch (this.idxStr) {
case 0: return string.Empty;
case 1: return this.s1;
case 2: return string.Concat(this.s1, this.s2);
case 3: return string.Concat(this.s1, this.s2, this.s3);
case 4: return string.Concat(this.s1, this.s2, this.s3, this.s4);
}
return string.Concat(this.strList.ToArray());
}
///
/// Concatenate a new string to the result without adding a delimiter.
///
internal void ConcatNoDelimiter(string s) {
switch (this.idxStr) {
case 0: this.s1 = s; break;
case 1: this.s2 = s; break;
case 2: this.s3 = s; break;
case 3: this.s4 = s; break;
case 4:
// Calling Clear() is expensive, allocate a new List instead
int capacity = (this.strList == null) ? 8 : this.strList.Count;
List strList = this.strList = new List(capacity);
strList.Add(this.s1);
strList.Add(this.s2);
strList.Add(this.s3);
strList.Add(this.s4);
goto default;
default:
this.strList.Add(s);
break;
}
this.idxStr++;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
namespace System.Xml.Xsl.Runtime {
///
/// Efficiently concatenates strings when the number of string is not known beforehand, and
/// yet it is common for only one string to be concatenated. StringBuilder is not good for
/// this purpose, since it *always* allocates objects, even if only one string is appended.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public struct StringConcat {
private string s1, s2, s3, s4;
private string delimiter;
private List strList;
int idxStr;
///
/// Clear the result string.
///
public void Clear() {
this.idxStr = 0;
this.delimiter = null;
}
///
/// Gets or sets the string that delimits concatenated strings.
///
public string Delimiter {
get { return this.delimiter; }
set { this.delimiter = value; }
}
///
/// Return the number of concatenated strings, including delimiters.
///
internal int Count {
get { return this.idxStr; }
}
///
/// Concatenate a new string to the result.
///
public void Concat(string value) {
Debug.Assert(value != null);
if (this.delimiter != null && this.idxStr != 0) {
// Add delimiter
ConcatNoDelimiter(this.delimiter);
}
ConcatNoDelimiter(value);
}
///
/// Get the result string.
///
public string GetResult() {
switch (this.idxStr) {
case 0: return string.Empty;
case 1: return this.s1;
case 2: return string.Concat(this.s1, this.s2);
case 3: return string.Concat(this.s1, this.s2, this.s3);
case 4: return string.Concat(this.s1, this.s2, this.s3, this.s4);
}
return string.Concat(this.strList.ToArray());
}
///
/// Concatenate a new string to the result without adding a delimiter.
///
internal void ConcatNoDelimiter(string s) {
switch (this.idxStr) {
case 0: this.s1 = s; break;
case 1: this.s2 = s; break;
case 2: this.s3 = s; break;
case 3: this.s4 = s; break;
case 4:
// Calling Clear() is expensive, allocate a new List instead
int capacity = (this.strList == null) ? 8 : this.strList.Count;
List strList = this.strList = new List(capacity);
strList.Add(this.s1);
strList.Add(this.s2);
strList.Add(this.s3);
strList.Add(this.s4);
goto default;
default:
this.strList.Add(s);
break;
}
this.idxStr++;
}
}
}
// 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
- _NtlmClient.cs
- StatusBarPanel.cs
- Soap.cs
- WsatExtendedInformation.cs
- XmlWriter.cs
- WebRequest.cs
- HtmlTableCell.cs
- HelpEvent.cs
- ConnectionString.cs
- CompositeCollectionView.cs
- TabControlEvent.cs
- Synchronization.cs
- XmlNodeComparer.cs
- SourceFilter.cs
- ServerValidateEventArgs.cs
- TimerEventSubscription.cs
- RuntimeHandles.cs
- UIElementIsland.cs
- PackagingUtilities.cs
- UserPrincipalNameElement.cs
- ButtonStandardAdapter.cs
- OleDbStruct.cs
- AuthenticationModuleElementCollection.cs
- UIElementPropertyUndoUnit.cs
- _DigestClient.cs
- PropertyEmitter.cs
- ContentElement.cs
- VirtualizingStackPanel.cs
- SmiMetaDataProperty.cs
- Comparer.cs
- ParameterToken.cs
- ExpressionBuilder.cs
- SettingsPropertyCollection.cs
- MatrixUtil.cs
- AmbientProperties.cs
- DispatcherTimer.cs
- ObjectDataSourceFilteringEventArgs.cs
- PeerCollaboration.cs
- WebPartsPersonalizationAuthorization.cs
- NominalTypeEliminator.cs
- CombinedGeometry.cs
- FileUpload.cs
- Vector3DAnimation.cs
- SpellerHighlightLayer.cs
- DragDrop.cs
- MatrixStack.cs
- SHA384.cs
- BaseTemplatedMobileComponentEditor.cs
- DataGridDesigner.cs
- EntityDataSourceChangedEventArgs.cs
- ModuleBuilder.cs
- DrawingContextWalker.cs
- SubclassTypeValidatorAttribute.cs
- Encoder.cs
- ApplicationSecurityManager.cs
- KeyProperty.cs
- BatchWriter.cs
- UpdatePanel.cs
- SerializationSectionGroup.cs
- _OverlappedAsyncResult.cs
- DataRelationPropertyDescriptor.cs
- AbstractSvcMapFileLoader.cs
- MatcherBuilder.cs
- TextBoxAutoCompleteSourceConverter.cs
- GridViewRow.cs
- CopyAttributesAction.cs
- XmlSerializationReader.cs
- DataListDesigner.cs
- CacheAxisQuery.cs
- KeyTimeConverter.cs
- IPHostEntry.cs
- SqlReorderer.cs
- RouteParametersHelper.cs
- HttpListenerRequestUriBuilder.cs
- MissingMemberException.cs
- RichTextBoxConstants.cs
- XmlCharType.cs
- ProviderConnectionPointCollection.cs
- RemotingConfigParser.cs
- PeerCollaborationPermission.cs
- CodeRegionDirective.cs
- FolderBrowserDialogDesigner.cs
- ListChangedEventArgs.cs
- EnlistmentTraceIdentifier.cs
- FaultBookmark.cs
- Block.cs
- AddInProcess.cs
- JournalEntryListConverter.cs
- NotCondition.cs
- StateRuntime.cs
- ResourcePermissionBaseEntry.cs
- ConfigurationConverterBase.cs
- Error.cs
- IndicCharClassifier.cs
- Image.cs
- ArcSegment.cs
- ImmComposition.cs
- VoiceSynthesis.cs
- EventLogger.cs
- TextFormatterHost.cs