本文整理汇总了C#中System.Runtime.InteropServices.StringBuffer类的典型用法代码示例。如果您正苦于以下问题:C# StringBuffer类的具体用法?C# StringBuffer怎么用?C# StringBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringBuffer类属于System.Runtime.InteropServices命名空间,在下文中一共展示了StringBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PerfTest
static void PerfTest()
{
var formatter = new StringBuffer();
var builder = new StringBuilder();
GC.Collect(2, GCCollectionMode.Forced, true);
var gcCount = GC.CollectionCount(0);
var timer = Stopwatch.StartNew();
for (int k = 0; k < mul; k++) {
for (int i = 0; i < count; i++)
formatter.AppendFormat(formatTest, v1, v2);
formatter.Clear();
}
timer.Stop();
Console.WriteLine("Mine : {0} us/format", timer.ElapsedMilliseconds * 1000.0 / (count * mul));
Console.WriteLine("GCs : {0}", GC.CollectionCount(0) - gcCount);
Console.WriteLine();
GC.Collect(2, GCCollectionMode.Forced, true);
gcCount = GC.CollectionCount(0);
timer = Stopwatch.StartNew();
for (int k = 0; k < mul; k++) {
for (int i = 0; i < count; i++)
builder.AppendFormat(formatTest, v1, v2);
builder.Clear();
}
timer.Stop();
Console.WriteLine("BCL : {0} us/format", timer.ElapsedMilliseconds * 1000.0 / (count * mul));
Console.WriteLine("GCs : {0}", GC.CollectionCount(0) - gcCount);
}
示例2: SetOverIndexThrowsArgumentOutOfRange
public void SetOverIndexThrowsArgumentOutOfRange()
{
using (var buffer = new StringBuffer())
{
Assert.Throws<ArgumentOutOfRangeException>(() => { buffer[0] = 'Q'; });
}
}
示例3: CustomFormat
static void CustomFormat(StringBuffer buffer, Blah blah, StringView format)
{
if (format == "yes")
buffer.Append("World!");
else
buffer.Append("(Goodbye)");
}
示例4: CanIndexChar
public void CanIndexChar()
{
using (var buffer = new StringBuffer())
{
buffer.Length = 1;
buffer[0] = 'Q';
Assert.Equal(buffer[0], 'Q');
}
}
示例5: IsExtended
/// <summary>
/// Returns true if the path uses the extended syntax (\\?\)
/// </summary>
internal static bool IsExtended(StringBuffer path)
{
// While paths like "//?/C:/" will work, they're treated the same as "\\.\" paths.
// Skipping of normalization will *only* occur if back slashes ('\') are used.
return path.Length >= DevicePrefixLength
&& path[0] == '\\'
&& (path[1] == '\\' || path[1] == '?')
&& path[2] == '?'
&& path[3] == '\\';
}
示例6: ReduceLength
public void ReduceLength()
{
using (var buffer = new StringBuffer("Food"))
{
Assert.Equal((ulong)5, buffer.CharCapacity);
buffer.Length = 3;
Assert.Equal("Foo", buffer.ToString());
// Shouldn't reduce capacity when dropping length
Assert.Equal((ulong)5, buffer.CharCapacity);
}
}
示例7: IsDevice
/// <summary>
/// Returns true if the path uses any of the DOS device path syntaxes. ("\\.\", "\\?\", or "\??\")
/// </summary>
internal static bool IsDevice(StringBuffer path)
{
// If the path begins with any two separators is will be recognized and normalized and prepped with
// "\??\" for internal usage correctly. "\??\" is recognized and handled, "/??/" is not.
return IsExtended(path)
||
(
path.Length >= DevicePrefixLength
&& IsDirectorySeparator(path[0])
&& IsDirectorySeparator(path[1])
&& (path[2] == '.' || path[2] == '?')
&& IsDirectorySeparator(path[3])
);
}
示例8: CreateFromString
public unsafe void CreateFromString()
{
string testString = "Test";
using (var buffer = new StringBuffer(testString))
{
Assert.Equal((ulong)testString.Length, buffer.Length);
Assert.Equal((ulong)testString.Length + 1, buffer.CharCapacity);
for (int i = 0; i < testString.Length; i++)
{
Assert.Equal(testString[i], buffer[(ulong)i]);
}
// Check the null termination
Assert.Equal('\0', buffer.CharPointer[testString.Length]);
Assert.Equal(testString, buffer.ToString());
}
}
示例9: Main
static void Main(string[] args)
{
var f = new StringBuffer();
f.AppendFormat(formatTest, v1, v2);
Console.WriteLine(f.ToString());
Console.WriteLine(formatTest, v1, v2);
// test custom formatters
StringBuffer.SetCustomFormatter<Blah>(CustomFormat);
f.Clear();
f.AppendFormat("Hello {0:yes}{0:no}", new Blah { Thing = 42 });
Console.WriteLine(f.ToString());
// test static convenience method
Console.WriteLine(StringBuffer.Format(formatTest, v1, v2));
PerfTest();
#if DEBUG
Console.ReadLine();
#endif
}
示例10: IsRelative
/// <summary>
/// Returns true if the path specified is relative to the current drive or working directory.
/// Returns false if the path is fixed to a specific drive or UNC path. This method does no
/// validation of the path (URIs will be returned as relative as a result).
/// </summary>
/// <remarks>
/// Handles paths that use the alternate directory separator. It is a frequent mistake to
/// assume that rooted paths (Path.IsPathRooted) are not relative. This isn't the case.
/// "C:a" is drive relative- meaning that it will be resolved against the current directory
/// for C: (rooted, but relative). "C:\a" is rooted and not relative (the current directory
/// will not be used to modify the path).
/// </remarks>
internal static bool IsRelative(StringBuffer path)
{
if (path.Length < 2)
{
// It isn't fixed, it must be relative. There is no way to specify a fixed
// path with one character (or less).
return true;
}
if (IsDirectorySeparator(path[0]))
{
// There is no valid way to specify a relative path with two initial slashes
return !IsDirectorySeparator(path[1]);
}
// The only way to specify a fixed path that doesn't begin with two slashes
// is the drive, colon, slash format- i.e. C:\
return !((path.Length >= 3)
&& (path[1] == Path.VolumeSeparatorChar)
&& IsDirectorySeparator(path[2]));
}
示例11: IsPartiallyQualified
/// <summary>
/// Returns true if the path specified is relative to the current drive or working directory.
/// Returns false if the path is fixed to a specific drive or UNC path. This method does no
/// validation of the path (URIs will be returned as relative as a result).
/// </summary>
/// <remarks>
/// Handles paths that use the alternate directory separator. It is a frequent mistake to
/// assume that rooted paths (Path.IsPathRooted) are not relative. This isn't the case.
/// "C:a" is drive relative- meaning that it will be resolved against the current directory
/// for C: (rooted, but relative). "C:\a" is rooted and not relative (the current directory
/// will not be used to modify the path).
/// </remarks>
internal static bool IsPartiallyQualified(StringBuffer path)
{
if (path.Length < 2)
{
// It isn't fixed, it must be relative. There is no way to specify a fixed
// path with one character (or less).
return true;
}
if (IsDirectorySeparator(path[0]))
{
// There is no valid way to specify a relative path with two initial slashes or
// \? as ? isn't valid for drive relative paths and \??\ is equivalent to \\?\
return !(path[1] == '?' || IsDirectorySeparator(path[1]));
}
// The only way to specify a fixed path that doesn't begin with two slashes
// is the drive, colon, slash format- i.e. C:\
return !((path.Length >= 3)
&& (path[1] == Path.VolumeSeparatorChar)
&& IsDirectorySeparator(path[2]));
}
示例12: GetFullPathName
private static void GetFullPathName(string path, StringBuffer fullPath)
{
// If the string starts with an extended prefix we would need to remove it from the path before we call GetFullPathName as
// it doesn't root extended paths correctly. We don't currently resolve extended paths, so we'll just assert here.
Debug.Assert(PathInternal.IsRelative(path) || !PathInternal.IsExtended(path));
// Historically we would skip leading spaces *only* if the path started with a drive " C:" or a UNC " \\"
int startIndex = PathInternal.PathStartSkip(path);
fixed (char* pathStart = path)
{
uint result = 0;
while ((result = Interop.mincore.GetFullPathNameW(pathStart + startIndex, (uint)fullPath.CharCapacity, fullPath.GetHandle(), IntPtr.Zero)) > fullPath.CharCapacity)
{
// Reported size (which does not include the null) is greater than the buffer size. Increase the capacity.
fullPath.EnsureCharCapacity(result);
}
if (result == 0)
{
// Failure, get the error and throw
int errorCode = Marshal.GetLastWin32Error();
if (errorCode == 0)
errorCode = Interop.mincore.Errors.ERROR_BAD_PATHNAME;
throw Win32Marshal.GetExceptionForWin32Error(errorCode, path);
}
fullPath.Length = result;
}
}
示例13: IsUnc
private static bool IsUnc(StringBuffer buffer)
{
return buffer.Length > 1 && buffer[0] == '\\' && buffer[1] == '\\';
}
示例14: TryExpandShortFileName
private static string TryExpandShortFileName(StringBuffer outputBuffer, string originalPath)
{
// We guarantee we'll expand short names for paths that only partially exist. As such, we need to find the part of the path that actually does exist. To
// avoid allocating like crazy we'll create only one input array and modify the contents with embedded nulls.
Debug.Assert(!PathInternal.IsPartiallyQualified(outputBuffer), "should have resolved by now");
// We'll have one of a few cases by now (the normalized path will have already:
//
// 1. Dos path (C:\)
// 2. Dos UNC (\\Server\Share)
// 3. Dos device path (\\.\C:\, \\?\C:\)
//
// We want to put the extended syntax on the front if it doesn't already have it, which may mean switching from \\.\.
//
// Note that we will never get \??\ here as GetFullPathName() does not recognize \??\ and will return it as C:\??\ (or whatever the current drive is).
uint rootLength = PathInternal.GetRootLength(outputBuffer);
bool isDevice = PathInternal.IsDevice(outputBuffer);
StringBuffer inputBuffer = null;
bool isDosUnc = false;
uint rootDifference = 0;
bool wasDotDevice = false;
// Add the extended prefix before expanding to allow growth over MAX_PATH
if (isDevice)
{
// We have one of the following (\\?\ or \\.\)
inputBuffer = new StringBuffer();
inputBuffer.Append(outputBuffer);
if (outputBuffer[2] == '.')
{
wasDotDevice = true;
inputBuffer[2] = '?';
}
}
else
{
isDosUnc = IsDosUnc(outputBuffer);
rootDifference = GetInputBuffer(outputBuffer, isDosUnc, out inputBuffer);
}
rootLength += rootDifference;
uint inputLength = inputBuffer.Length;
bool success = false;
uint foundIndex = inputBuffer.Length - 1;
while (!success)
{
uint result = Interop.mincore.GetLongPathNameW(inputBuffer.GetHandle(), outputBuffer.GetHandle(), outputBuffer.CharCapacity);
// Replace any temporary null we added
if (inputBuffer[foundIndex] == '\0') inputBuffer[foundIndex] = '\\';
if (result == 0)
{
// Look to see if we couldn't find the file
int error = Marshal.GetLastWin32Error();
if (error != Interop.mincore.Errors.ERROR_FILE_NOT_FOUND && error != Interop.mincore.Errors.ERROR_PATH_NOT_FOUND)
{
// Some other failure, give up
break;
}
// We couldn't find the path at the given index, start looking further back in the string.
foundIndex--;
for (; foundIndex > rootLength && inputBuffer[foundIndex] != '\\'; foundIndex--) ;
if (foundIndex == rootLength)
{
// Can't trim the path back any further
break;
}
else
{
// Temporarily set a null in the string to get Windows to look further up the path
inputBuffer[foundIndex] = '\0';
}
}
else if (result > outputBuffer.CharCapacity)
{
// Not enough space. The result count for this API does not include the null terminator.
outputBuffer.EnsureCharCapacity(result);
result = Interop.mincore.GetLongPathNameW(inputBuffer.GetHandle(), outputBuffer.GetHandle(), outputBuffer.CharCapacity);
}
else
{
// Found the path
success = true;
outputBuffer.Length = result;
if (foundIndex < inputLength - 1)
{
// It was a partial find, put the non-existent part of the path back
outputBuffer.Append(inputBuffer, foundIndex, inputBuffer.Length - foundIndex);
}
}
}
//.........这里部分代码省略.........
示例15: IsDosUnc
private static bool IsDosUnc(StringBuffer buffer)
{
return !PathInternal.IsDevice(buffer) && buffer.Length > 1 && buffer[0] == '\\' && buffer[1] == '\\';
}