public bool TryFormat(Span <char> destination, int fieldCount, out int charsWritten) { if (fieldCount == 0) { charsWritten = 0; return(true); } else if (fieldCount == 1) { return(_Major.TryFormat(destination, out charsWritten)); } StringBuilder sb = ToCachedStringBuilder(fieldCount); if (sb.Length <= destination.Length) { sb.CopyTo(0, destination, sb.Length); StringBuilderCache.Release(sb); charsWritten = sb.Length; return(true); } StringBuilderCache.Release(sb); charsWritten = 0; return(false); }
private static string ExpandEnvironmentVariablesCore(string name) { int currentSize = 100; StringBuilder result = StringBuilderCache.Acquire(currentSize); // A somewhat reasonable default size result.Length = 0; int size = Interop.Kernel32.ExpandEnvironmentStringsW(name, result, currentSize); if (size == 0) { StringBuilderCache.Release(result); Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } while (size > currentSize) { currentSize = size; result.Capacity = currentSize; result.Length = 0; size = Interop.Kernel32.ExpandEnvironmentStringsW(name, result, currentSize); if (size == 0) { StringBuilderCache.Release(result); Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } return(StringBuilderCache.GetStringAndRelease(result)); }
private static string GetEnvironmentVariableCore(string variable) { if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode()) { // Environment variable accessors are not approved modern API. // Behave as if the variable was not found in this case. return(null); } StringBuilder sb = StringBuilderCache.Acquire(128); // A somewhat reasonable default size int requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity); if (requiredSize == 0 && Marshal.GetLastWin32Error() == Win32Native.ERROR_ENVVAR_NOT_FOUND) { StringBuilderCache.Release(sb); return(null); } while (requiredSize > sb.Capacity) { sb.Capacity = requiredSize; sb.Length = 0; requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity); } return(StringBuilderCache.GetStringAndRelease(sb)); }
private static string GetEnvironmentVariableCore(string variable) { StringBuilder sb = StringBuilderCache.Acquire(128); // A somewhat reasonable default size int requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity); if (requiredSize == 0 && Marshal.GetLastWin32Error() == Win32Native.ERROR_ENVVAR_NOT_FOUND) { StringBuilderCache.Release(sb); return(null); } while (requiredSize > sb.Capacity) { sb.Capacity = requiredSize; sb.Length = 0; requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity); } return(StringBuilderCache.GetStringAndRelease(sb)); }
public bool TryFormat(Span <char> destination, int fieldCount, out int charsWritten) { if (fieldCount == 0) { charsWritten = 0; return(true); } // TODO https://github.com/dotnet/corefx/issues/22403: fieldCount==1 can just use int.TryFormat StringBuilder sb = ToCachedStringBuilder(fieldCount); if (sb.Length <= destination.Length) { sb.CopyTo(0, destination, sb.Length); StringBuilderCache.Release(sb); charsWritten = sb.Length; return(true); } StringBuilderCache.Release(sb); charsWritten = 0; return(false); }
/// <summary> /// Helper function for retrieving a localized string resource via MUI. /// The function expects a string in the form: "@resource.dll, -123" /// /// "resource.dll" is a language-neutral portable executable (LNPE) file in /// the %windir%\system32 directory. The OS is queried to find the best-fit /// localized resource file for this LNPE (ex: %windir%\system32\en-us\resource.dll.mui). /// If a localized resource file exists, we LoadString resource ID "123" and /// return it to our caller. /// </summary> private static string TryGetLocalizedNameByMuiNativeResource(string resource) { if (string.IsNullOrEmpty(resource)) { return(string.Empty); } // parse "@tzres.dll, -100" // // filePath = "C:\Windows\System32\tzres.dll" // resourceId = -100 // string[] resources = resource.Split(','); if (resources.Length != 2) { return(string.Empty); } string filePath; int resourceId; // get the path to Windows\System32 string system32 = Environment.SystemDirectory; // trim the string "@tzres.dll" => "tzres.dll" string tzresDll = resources[0].TrimStart('@'); try { filePath = Path.Combine(system32, tzresDll); } catch (ArgumentException) { // there were probably illegal characters in the path return(string.Empty); } if (!int.TryParse(resources[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out resourceId)) { return(string.Empty); } resourceId = -resourceId; try { StringBuilder fileMuiPath = StringBuilderCache.Acquire(Path.MaxPath); fileMuiPath.Length = Path.MaxPath; int fileMuiPathLength = Path.MaxPath; int languageLength = 0; long enumerator = 0; bool succeeded = UnsafeNativeMethods.GetFileMUIPath( Win32Native.MUI_PREFERRED_UI_LANGUAGES, filePath, null /* language */, ref languageLength, fileMuiPath, ref fileMuiPathLength, ref enumerator); if (!succeeded) { StringBuilderCache.Release(fileMuiPath); return(string.Empty); } return(TryGetLocalizedNameByNativeResource(StringBuilderCache.GetStringAndRelease(fileMuiPath), resourceId)); } catch (EntryPointNotFoundException) { return(string.Empty); } }
private static string InternalFlagsFormat(RuntimeType eT, TypeValuesAndNames entry, ulong result) { Debug.Assert(eT != null); string[] names = entry.Names; ulong[] values = entry.Values; Debug.Assert(names.Length == values.Length); int index = values.Length - 1; StringBuilder sb = StringBuilderCache.Acquire(); bool firstTime = true; ulong saveResult = result; // We will not optimize this code further to keep it maintainable. There are some boundary checks that can be applied // to minimize the comparsions required. This code works the same for the best/worst case. In general the number of // items in an enum are sufficiently small and not worth the optimization. while (index >= 0) { if ((index == 0) && (values[index] == 0)) { break; } if ((result & values[index]) == values[index]) { result -= values[index]; if (!firstTime) { sb.Insert(0, enumSeparatorString); } sb.Insert(0, names[index]); firstTime = false; } index--; } string returnString; if (result != 0) { // We were unable to represent this number as a bitwise or of valid flags returnString = null; // return null so the caller knows to .ToString() the input } else if (saveResult == 0) { // For the cases when we have zero if (values.Length > 0 && values[0] == 0) { returnString = names[0]; // Zero was one of the enum values. } else { returnString = "0"; } } else { returnString = sb.ToString(); // Return the string representation } StringBuilderCache.Release(sb); return(returnString); }