public static string ConvertDateTimeToHttpString(DateTime dt) { if (dt != DateTime.MaxValue && dt != DateTime.MinValue) { NephosAssertionException.Assert(dt.Kind == DateTimeKind.Utc); } StringBuilder stringBuilder = StringBuilderPool.Allocate(); stringBuilder.Append(HttpUtilities.DaysOfWeek[(int)dt.DayOfWeek]); stringBuilder.Append(", "); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Day, (long)10); stringBuilder.Append(' '); stringBuilder.Append(HttpUtilities.MonthsOfYear[dt.Month]); stringBuilder.Append(' '); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Year, (long)1000); stringBuilder.Append(' '); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Hour, (long)10); stringBuilder.Append(':'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Minute, (long)10); stringBuilder.Append(':'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Second, (long)10); stringBuilder.Append(" GMT"); string str = stringBuilder.ToString(); StringBuilderPool.Release(stringBuilder); return(str); }
private void Write(LogPayload logPayload) { ConsoleColor consoleColor = Console.ForegroundColor; var stringBuilder = StringBuilderPool.Acquire(); stringBuilder.Append(DateTime.UtcNow.ToString(DateTimeConsoleFormat, CultureInfo.CurrentCulture)); stringBuilder.Append(" "); stringBuilder.Append(("[" + logPayload.LogLevel.Name + "]").PadRight(8)); stringBuilder.Append(logPayload.Name); Console.ForegroundColor = ConsoleColor.Gray; Console.Out.WriteLine(stringBuilder.ToString()); StringBuilderPool.Release(stringBuilder); Console.ForegroundColor = FetchColorByLevel(logPayload.LogLevel); Console.Out.WriteLine(logPayload.Message); if (logPayload.Callstack != null) { Console.Out.WriteLine(logPayload.Callstack); } Console.ForegroundColor = consoleColor; }
public static string ConvertSnapshotDateTimeToHttpString(DateTime dt) { if (dt != DateTime.MaxValue && dt != DateTime.MinValue) { NephosAssertionException.Assert(dt.Kind == DateTimeKind.Utc); } StringBuilder stringBuilder = StringBuilderPool.Allocate(); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Year, (long)1000); stringBuilder.Append('-'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Month, (long)10); stringBuilder.Append('-'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Day, (long)10); stringBuilder.Append('T'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Hour, (long)10); stringBuilder.Append(':'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Minute, (long)10); stringBuilder.Append(':'); HttpUtilities.AppendDigits(stringBuilder, (long)dt.Second, (long)10); stringBuilder.Append('.'); HttpUtilities.AppendDigits(stringBuilder, dt.Ticks % (long)10000000, (long)1000000); stringBuilder.Append('Z'); string str = stringBuilder.ToString(); StringBuilderPool.Release(stringBuilder); return(str); }
private static void runCreateWallet() { Console.WriteLine("Creating wallet."); Console.WriteLine("\tEsc to cancel, Backspace to reset, Enter to confirm."); var sb = StringBuilderPool.Acquire(); while (true) { sb.Clear(); Console.Write("\tPassphrase: "); var passphrase = readPassphrase(sb); if (passphrase == null) { break; } if (passphrase.Length == 0) { continue; } _app.CreateWallet(passphrase); Console.WriteLine("Wallet created."); break; } StringBuilderPool.Release(sb); runLogin(); }
public static void LogNewArchetype(ChunkTrace chunk) { StringBuilder stringBuilder = StringBuilderPool.Take(); stringBuilder.Append("new "); ArchetypeToString(stringBuilder, chunk); LogChange(stringBuilder.ToString()); StringBuilderPool.Release(stringBuilder); }
public static void LogDestroyedEntities(ChunkTrace chunk) { StringBuilder stringBuilder = StringBuilderPool.Take(); stringBuilder.Append("destroyed entity/entities of "); ArchetypeToString(stringBuilder, chunk); LogChange(stringBuilder.ToString()); StringBuilderPool.Release(stringBuilder); }
public static void LogModifiedComponent(ChunkTrace chunk, ComponentType type) { StringBuilder stringBuilder = StringBuilderPool.Take(); stringBuilder.Append("modified component "); stringBuilder.Append(type); stringBuilder.Append(" of "); ArchetypeToString(stringBuilder, chunk); LogChange(stringBuilder.ToString()); StringBuilderPool.Release(stringBuilder); }
protected string InsertAdditionalUsings(string scriptContent, string[] additionalUsings) { StringBuilder stringBuilder = StringBuilderPool.Take(); foreach (string additionalUsing in additionalUsings) { if (!scriptContent.Contains(additionalUsing)) { stringBuilder.AppendLine(additionalUsing); } } stringBuilder.AppendLine(scriptContent); string result = stringBuilder.ToString(); StringBuilderPool.Release(stringBuilder); return(result); }
private static void runLogin() { Console.WriteLine("Log-in wallet."); Console.WriteLine("\tEsc to cancel, Backspace to reset, Enter to confirm."); var sb = StringBuilderPool.Acquire(); while (true) { Console.Write("\tPassphrase: "); var passphrase = readPassphrase(sb); if (passphrase == null) { break; } if (passphrase.Length == 0) { continue; } bool isLoggedIn = _app.LoginWallet(passphrase); if (isLoggedIn) { break; } else { Console.WriteLine("Invalid passphrase. Please try again."); } } StringBuilderPool.Release(sb); if (_app.WalletLoggedIn) { Console.WriteLine("Login successful."); Console.WriteLine("Your address is {0}", _app.WalletAddress); } }
public void TestPoolRelease() { var pool = new StringBuilderPool(); pool.Initialize(); // allocate a new instance using this custom factory, make sure the factory code got called var sb1 = pool.Allocate(); sb1.Append("abc"); Assert.IsTrue(pool.UsedListSize == 1); Assert.IsTrue(pool.FreeListSize == 0); // allocate and free an instance. this will grow the pool to size one Assert.IsTrue(pool.ProduceValueAndFree(pool.Allocate()) == ""); Assert.IsTrue(pool.UsedListSize == 1); Assert.IsTrue(pool.FreeListSize == 1); // now release the entire object pool. this will clear out the free list, but will not // affect the object already in circulation, since we're not tracking them. pool.Release(); Assert.IsTrue(sb1.Length == 3); Assert.IsTrue(pool.UsedListSize == 1); Assert.IsTrue(pool.FreeListSize == -1); }
public void AddMessage(int _actorID, string _message) { if (_message.IsNullOrEmpty() && _message == null) { _message = string.Empty; } OneColor oneColor = this.PersonColor(_actorID); string self = this.PersonName(_actorID) ?? string.Empty; if (self.IsNullOrEmpty() && _message.IsNullOrEmpty()) { Debug.LogWarning((object)"AddNotify: キャラ名もメッセージも空っぽだったので追加せず"); } else if (self.IsNullOrEmpty()) { this.AddLog(_message); } else { if ((string)oneColor != (string)null) { StringBuilder toRelease = StringBuilderPool.Get(); toRelease.AppendFormat("<color={0}>{1}</color>{2}", (object)oneColor, (object)self, (object)_message); _message = toRelease.ToString(); StringBuilderPool.Release(toRelease); } else { StringBuilder toRelease = StringBuilderPool.Get(); toRelease.AppendFormat("{0}{1}", (object)self, (object)_message); _message = toRelease.ToString(); StringBuilderPool.Release(toRelease); } this.AddLog(_message); } }
public static Guid GetCharaGuid(this SaveData.CharaData charaData, int guidVersion = PluginDataInfo.CurrentCharaGuidVersion) { if (guidVersion > PluginDataInfo.MaxCharaGuidVersion) { throw new ArgumentOutOfRangeException(nameof(guidVersion), $"Unknown guidVersion ({guidVersion})"); } if (guidVersion < PluginDataInfo.MinimumSupportedCharaGuidVersion || charaData == null || !charaData.charFileInitialized) { return(Guid.Empty); } var guidKeyBuilder = StringBuilderPool.Get(); var intFmt = "{0:04}"; try { switch (guidVersion) { case 5: guidKeyBuilder .AppendFormat(intFmt, charaData is SaveData.Heroine heroine5 ? heroine5.FixCharaIDOrPersonality : charaData.personality) .Append('/') .AppendFormat(intFmt, charaData.schoolClass) .Append('/') .AppendFormat(intFmt, charaData.schoolClassIndex) .Append('/') .Append(charaData.Name); break; case 6: guidKeyBuilder .AppendFormat(intFmt, charaData is SaveData.Heroine heroine6 ? heroine6.FixCharaIDOrPersonality : charaData.personality) .Append('/') .AppendFormat(intFmt, charaData.schoolClass) .Append('/') .AppendFormat(intFmt, charaData.schoolClassIndex) .Append('/') .Append(charaData.firstname) .Append('/') .Append(charaData.lastname) .Append('/') .Append(charaData.parameter.sex); break; default: throw new ArgumentOutOfRangeException(nameof(guidVersion), $"Unsupported guidVersion ({guidVersion})"); } if (guidKeyBuilder.Length == 0) { return(Guid.Empty); } var guidKey = guidKeyBuilder.ToString(); var result = GuidCache.Get(guidKey); Logger?.DebugLogDebug( $"{nameof(GetCharaGuid)} (version={guidVersion}): guidKey={guidKey}, result={result}"); return(result); } finally { StringBuilderPool.Release(guidKeyBuilder); } }
/// <summary> /// <para>Turns a TimeSpan into a human-readable text.</para> /// <para>Uses the specified timeSpanFormatOptions.</para> /// <para>For example: "31.23:59:00.555" = "31 days 23 hours 59 minutes 0 seconds 555 milliseconds"</para> /// </summary> /// <param name="FromTime"></param> /// <param name="options"> /// <para>A combination of flags that determine the formatting options.</para> /// <para>These will be combined with the default timeSpanFormatOptions.</para> /// </param> /// <param name="timeTextInfo">An object that supplies the text to use for output</param> public static string ToTimeString(this TimeSpan FromTime, TimeSpanFormatOptions options, TimeTextInfo timeTextInfo) { // If there are any missing options, merge with the defaults: // Also, as a safeguard against missing DefaultFormatOptions, let's also merge with the AbsoluteDefaults: options = options.Merge(DefaultFormatOptions).Merge(AbsoluteDefaults); // Extract the individual options: var rangeMax = options.Mask(RangeAll).AllFlags().Last(); var rangeMin = options.Mask(RangeAll).AllFlags().First(); var truncate = options.Mask(TruncateAll).AllFlags().First(); var lessThan = options.Mask(LessThanAll) != TimeSpanFormatOptions.LessThanOff; var abbreviate = options.Mask(AbbreviateAll) != TimeSpanFormatOptions.AbbreviateOff; var round = lessThan ? (Func <double, double>)Math.Floor : Math.Ceiling; switch (rangeMin) { case TimeSpanFormatOptions.RangeWeeks: FromTime = TimeSpan.FromDays(round(FromTime.TotalDays / 7) * 7); break; case TimeSpanFormatOptions.RangeDays: FromTime = TimeSpan.FromDays(round(FromTime.TotalDays)); break; case TimeSpanFormatOptions.RangeHours: FromTime = TimeSpan.FromHours(round(FromTime.TotalHours)); break; case TimeSpanFormatOptions.RangeMinutes: FromTime = TimeSpan.FromMinutes(round(FromTime.TotalMinutes)); break; case TimeSpanFormatOptions.RangeSeconds: FromTime = TimeSpan.FromSeconds(round(FromTime.TotalSeconds)); break; case TimeSpanFormatOptions.RangeMilliSeconds: FromTime = TimeSpan.FromMilliseconds(round(FromTime.TotalMilliseconds)); break; } // Create our result: var textStarted = false; var result = StringBuilderPool.Get(); for (var i = rangeMax; i >= rangeMin; i = (TimeSpanFormatOptions)((int)i >> 1)) { // Determine the value and title: int value; switch (i) { case TimeSpanFormatOptions.RangeWeeks: value = (int)Math.Floor(FromTime.TotalDays / 7); FromTime -= TimeSpan.FromDays(value * 7); break; case TimeSpanFormatOptions.RangeDays: value = (int)Math.Floor(FromTime.TotalDays); FromTime -= TimeSpan.FromDays(value); break; case TimeSpanFormatOptions.RangeHours: value = (int)Math.Floor(FromTime.TotalHours); FromTime -= TimeSpan.FromHours(value); break; case TimeSpanFormatOptions.RangeMinutes: value = (int)Math.Floor(FromTime.TotalMinutes); FromTime -= TimeSpan.FromMinutes(value); break; case TimeSpanFormatOptions.RangeSeconds: value = (int)Math.Floor(FromTime.TotalSeconds); FromTime -= TimeSpan.FromSeconds(value); break; case TimeSpanFormatOptions.RangeMilliSeconds: value = (int)Math.Floor(FromTime.TotalMilliseconds); FromTime -= TimeSpan.FromMilliseconds(value); break; default: // This code is unreachable, but it prevents compile-errors. throw new ArgumentException("TimeSpanUtility"); } //Determine whether to display this value var displayThisValue = false; var breakFor = false; // I wish C# supported "break for;" (like how VB supports "Exit For" from within a "Select Case" statement) switch (truncate) { case TimeSpanFormatOptions.TruncateShortest: if (textStarted) { breakFor = true; break; } if (value > 0) { displayThisValue = true; } break; case TimeSpanFormatOptions.TruncateAuto: if (value > 0) { displayThisValue = true; } break; case TimeSpanFormatOptions.TruncateFill: if (textStarted || value > 0) { displayThisValue = true; } break; case TimeSpanFormatOptions.TruncateFull: displayThisValue = true; break; } if (breakFor) { break; } //we need to display SOMETHING (even if it's zero) if (i == rangeMin && textStarted == false) { displayThisValue = true; if (lessThan && value < 1) { // Output the "less than 1 unit" text: var unitTitle = timeTextInfo.GetUnitText(rangeMin, 1, abbreviate); result.Append(timeTextInfo.GetLessThanText(unitTitle)); displayThisValue = false; } } // Output the value: if (displayThisValue) { if (textStarted) { result.Append(" "); } var unitTitle = timeTextInfo.GetUnitText(i, value, abbreviate); result.Append(unitTitle); textStarted = true; } } var ret = result.ToString(); StringBuilderPool.Release(result); return(ret); }
internal static void ReleaseStringBuilder(StringBuilder stringBuilder) { #if GEBO_COMMON_FULL StringBuilderPool.Release(stringBuilder); #endif }