Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow usage of string.To{Upper,Lower}() without explicit culture #18834

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CodeAnalysis/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ M:Realms.CollectionExtensions.SubscribeForNotifications`1(System.Collections.Gen
M:System.Threading.Tasks.Task.Wait();Don't use Task.Wait. Use Task.WaitSafely() to ensure we avoid deadlocks.
P:System.Threading.Tasks.Task`1.Result;Don't use Task.Result. Use Task.GetResultSafely() to ensure we avoid deadlocks.
M:System.Threading.ManualResetEventSlim.Wait();Specify a timeout to avoid waiting forever.
M:System.String.ToLower();string.ToLower() changes behaviour depending on CultureInfo.CurrentCulture. Use string.ToLowerInvariant() instead. If wanting culture-sensitive behaviour, explicitly provide CultureInfo.CurrentCulture or use LocalisableString.
M:System.String.ToUpper();string.ToUpper() changes behaviour depending on CultureInfo.CurrentCulture. Use string.ToUpperInvariant() instead. If wanting culture-sensitive behaviour, explicitly provide CultureInfo.CurrentCulture or use LocalisableString.
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Catch/CatchSkinComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public CatchSkinComponent(CatchSkinComponents component)

protected override string RulesetPrefix => "catch"; // todo: use CatchRuleset.SHORT_NAME;

protected override string ComponentName => Component.ToString().ToLower();
protected override string ComponentName => Component.ToString().ToLowerInvariant();
}
}
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Mania/ManiaSkinComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ManiaSkinComponent(ManiaSkinComponents component, StageDefinition? stageD

protected override string RulesetPrefix => ManiaRuleset.SHORT_NAME;

protected override string ComponentName => Component.ToString().ToLower();
protected override string ComponentName => Component.ToString().ToLowerInvariant();
}

public enum ManiaSkinComponents
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Osu/OsuSkinComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public OsuSkinComponent(OsuSkinComponents component)

protected override string RulesetPrefix => OsuRuleset.SHORT_NAME;

protected override string ComponentName => Component.ToString().ToLower();
protected override string ComponentName => Component.ToString().ToLowerInvariant();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void TestClearStateOnClearedSwell(bool kiai, TaikoMascotAnimationState ex
createDrawableRuleset();

assertStateAfterResult(new JudgementResult(new Swell(), new TaikoSwellJudgement()) { Type = HitResult.Great }, TaikoMascotAnimationState.Clear);
AddUntilStep($"state reverts to {expectedStateAfterClear.ToString().ToLower()}", () => allMascotsIn(expectedStateAfterClear));
AddUntilStep($"state reverts to {expectedStateAfterClear.ToString().ToLowerInvariant()}", () => allMascotsIn(expectedStateAfterClear));
}

private void setBeatmap(bool kiai = false)
Expand Down Expand Up @@ -195,7 +195,7 @@ private void assertStateAfterResult(JudgementResult judgementResult, TaikoMascot
{
TaikoMascotAnimationState[] mascotStates = null;

AddStep($"{judgementResult.Type.ToString().ToLower()} result for {judgementResult.Judgement.GetType().Name.Humanize(LetterCasing.LowerCase)}",
AddStep($"{judgementResult.Type.ToString().ToLowerInvariant()} result for {judgementResult.Judgement.GetType().Name.Humanize(LetterCasing.LowerCase)}",
() =>
{
applyNewResult(judgementResult);
Expand All @@ -204,7 +204,7 @@ private void assertStateAfterResult(JudgementResult judgementResult, TaikoMascot
Schedule(() => mascotStates = animatedMascots.Select(mascot => mascot.State.Value).ToArray());
});

AddAssert($"state is {expectedState.ToString().ToLower()}", () => mascotStates.All(state => state == expectedState));
AddAssert($"state is {expectedState.ToString().ToLowerInvariant()}", () => mascotStates.All(state => state == expectedState));
}

private void applyNewResult(JudgementResult judgementResult)
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Taiko/TaikoSkinComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public TaikoSkinComponent(TaikoSkinComponents component)

protected override string RulesetPrefix => TaikoRuleset.SHORT_NAME;

protected override string ComponentName => Component.ToString().ToLower();
protected override string ComponentName => Component.ToString().ToLowerInvariant();
}
}
4 changes: 2 additions & 2 deletions osu.Game.Rulesets.Taiko/UI/TaikoMascotAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ private void load(ISkinSource source)

private static Texture getAnimationFrame(ISkin skin, TaikoMascotAnimationState state, int frameIndex)
{
var texture = skin.GetTexture($"pippidon{state.ToString().ToLower()}{frameIndex}");
var texture = skin.GetTexture($"pippidon{state.ToString().ToLowerInvariant()}{frameIndex}");

if (frameIndex == 0 && texture == null)
texture = skin.GetTexture($"pippidon{state.ToString().ToLower()}");
texture = skin.GetTexture($"pippidon{state.ToString().ToLowerInvariant()}");

return texture;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void TestMultipleFailConditions(HitResult resultApplied)
AddStep("apply perfect hit result", () => processor.ApplyResult(new JudgementResult(beatmap.HitObjects[0], new Judgement()) { Type = HitResult.Perfect }));
AddAssert("not failed", () => !processor.HasFailed);

AddStep($"apply {resultApplied.ToString().ToLower()} hit result", () => processor.ApplyResult(new JudgementResult(beatmap.HitObjects[0], new Judgement()) { Type = resultApplied }));
AddStep($"apply {resultApplied.ToString().ToLowerInvariant()} hit result", () => processor.ApplyResult(new JudgementResult(beatmap.HitObjects[0], new Judgement()) { Type = resultApplied }));
AddAssert("failed", () => processor.HasFailed);
}

Expand Down
4 changes: 2 additions & 2 deletions osu.Game.Tournament/Models/TournamentTeam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public TournamentTeam()
{
// use a sane default flag name based on acronym.
if (val.OldValue.StartsWith(FlagName.Value, StringComparison.InvariantCultureIgnoreCase))
FlagName.Value = val.NewValue.Length >= 2 ? val.NewValue?.Substring(0, 2).ToUpper() : string.Empty;
FlagName.Value = val.NewValue.Length >= 2 ? val.NewValue?.Substring(0, 2).ToUpperInvariant() : string.Empty;
};

FullName.ValueChanged += val =>
{
// use a sane acronym based on full name.
if (val.OldValue.StartsWith(Acronym.Value, StringComparison.InvariantCultureIgnoreCase))
Acronym.Value = val.NewValue.Length >= 3 ? val.NewValue?.Substring(0, 3).ToUpper() : string.Empty;
Acronym.Value = val.NewValue.Length >= 3 ? val.NewValue?.Substring(0, 3).ToUpperInvariant() : string.Empty;
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public DrawableTournamentRound(TournamentRound round, bool losers = false)
};

name = round.Name.GetBoundCopy();
name.BindValueChanged(n => textName.Text = ((losers ? "Losers " : "") + round.Name).ToUpper(), true);
name.BindValueChanged(n => textName.Text = ((losers ? "Losers " : "") + round.Name).ToUpperInvariant(), true);

description = round.Description.GetBoundCopy();
description.BindValueChanged(n => textDescription.Text = round.Description.Value?.ToUpper(), true);
description.BindValueChanged(n => textDescription.Text = round.Description.Value?.ToUpperInvariant(), true);
}
}
}
2 changes: 1 addition & 1 deletion osu.Game.Tournament/Screens/TeamIntro/SeedingScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void load(TextureStore textures)
{
row.Add(new Sprite
{
Texture = textures.Get($"Mods/{mods.ToLower()}"),
Texture = textures.Get($"Mods/{mods.ToLowerInvariant()}"),
Scale = new Vector2(0.5f)
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void toggleFavouriteStatus()
};
favouriteRequest.Failure += e =>
{
Logger.Error(e, $"Failed to {actionType.ToString().ToLower()} beatmap: {e.Message}");
Logger.Error(e, $"Failed to {actionType.ToString().ToLowerInvariant()} beatmap: {e.Message}");
Enabled.Value = true;
};

Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Database/IModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IModelImporter<TModel> : IPostNotifications, ICanAcceptFiles
/// <summary>
/// A user displayable name for the model type associated with this manager.
/// </summary>
string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLower()}";
string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLowerInvariant()}";

/// <summary>
/// Fired when the user requests to view the resulting import.
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Database/ModelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,6 @@ public void Undelete(TModel item)

public Action<Notification>? PostNotification { get; set; }

public virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLower()}";
public virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLowerInvariant()}";
}
}
2 changes: 1 addition & 1 deletion osu.Game/Database/RealmArchiveModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,6 @@ private IEnumerable<string> getFilenames(IEnumerable<INamedFile> files)
yield return f.Filename;
}

public virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLower()}";
public virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLowerInvariant()}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override WebRequest CreateWebRequest()
var req = base.CreateWebRequest();

req.AddParameter("spotlight", spotlight.ToString());
req.AddParameter("filter", sort.ToString().ToLower());
req.AddParameter("filter", sort.ToString().ToLowerInvariant());

return req;
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Online/API/Requests/GetUserRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public GetUserRequest(string username = null, IRulesetInfo ruleset = null)
Ruleset = ruleset;
}

protected override string Target => Lookup != null ? $@"users/{Lookup}/{Ruleset?.ShortName}?key={lookupType.ToString().ToLower()}" : $@"me/{Ruleset?.ShortName}";
protected override string Target => Lookup != null ? $@"users/{Lookup}/{Ruleset?.ShortName}?key={lookupType.ToString().ToLowerInvariant()}" : $@"me/{Ruleset?.ShortName}";

private enum LookupType
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Online/Leaderboards/UserTopScoreContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public UserTopScoreContainer(Func<TScoreInfo, LeaderboardScore> createScoreDeleg
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = @"your personal best".ToUpper(),
Text = @"your personal best".ToUpperInvariant(),
Font = OsuFont.GetFont(size: 15, weight: FontWeight.Bold),
},
scoreContainer = new Container
Expand Down
3 changes: 2 additions & 1 deletion osu.Game/Overlays/Chat/DaySeparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using osu.Framework.Allocation;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
Expand Down Expand Up @@ -79,7 +80,7 @@ private void load()
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Text = time.ToLocalTime().ToString("dd MMMM yyyy").ToUpper(),
Text = time.ToLocalTime().ToLocalisableString(@"dd MMMM yyyy").ToUpper(),
Font = OsuFont.Torus.With(size: TextSize, weight: FontWeight.SemiBold),
Colour = colourProvider?.Content1 ?? Colour4.White,
},
Expand Down
3 changes: 2 additions & 1 deletion osu.Game/Overlays/News/NewsCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
Expand Down Expand Up @@ -146,7 +147,7 @@ private void load(OverlayColourProvider colourProvider)
},
new OsuSpriteText
{
Text = date.ToString("d MMM yyyy").ToUpper(),
Text = date.ToLocalisableString(@"d MMM yyyy").ToUpper(),
Font = OsuFont.GetFont(size: 10, weight: FontWeight.SemiBold),
Margin = new MarginPadding
{
Expand Down
6 changes: 3 additions & 3 deletions osu.Game/Overlays/TabControlOverlayHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public OverlayHeaderTabItem(T value)
: base(value)
{
if (!(Value is Enum enumValue))
Text.Text = Value.ToString().ToLower();
Text.Text = Value.ToString().ToLowerInvariant();
else
{
var localisableDescription = enumValue.GetLocalisableDescription();
string nonLocalisableDescription = enumValue.GetDescription();

// If localisable == non-localisable, then we must have a basic string, so .ToLower() is used.
// If localisable == non-localisable, then we must have a basic string, so .ToLowerInvariant() is used.
Text.Text = localisableDescription.Equals(nonLocalisableDescription)
? nonLocalisableDescription.ToLower()
? nonLocalisableDescription.ToLowerInvariant()
: localisableDescription;
}

Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Rulesets/Edit/Checks/CheckMutedObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private IEnumerable<Issue> getVolumeIssues(HitObject hitObject, HitObject sample
if (edgeType == EdgeType.None)
yield break;

string postfix = hitObject is IHasDuration ? edgeType.ToString().ToLower() : null;
string postfix = hitObject is IHasDuration ? edgeType.ToString().ToLowerInvariant() : null;

if (maxVolume <= muted_threshold)
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Rulesets/Edit/Checks/CheckTooShortAudioFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IEnumerable<Issue> Run(BeatmapVerifierContext context)
}
}

private bool hasAudioExtension(string filename) => audioExtensions.Any(filename.ToLower().EndsWith);
private bool hasAudioExtension(string filename) => audioExtensions.Any(filename.ToLowerInvariant().EndsWith);
private bool probablyHasAudioData(Stream data) => data.Length > min_bytes_threshold;

public class IssueTemplateTooShort : IssueTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Section(string title)
new OsuSpriteText
{
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 12),
Text = title.ToUpper(),
Text = title.ToUpperInvariant(),
},
content = new Container
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Screens/Select/FilterQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static void ApplyQueries(FilterCriteria criteria, string query)
{
foreach (Match match in query_syntax_regex.Matches(query))
{
string key = match.Groups["key"].Value.ToLower();
string key = match.Groups["key"].Value.ToLowerInvariant();
var op = parseOperator(match.Groups["op"].Value);
string value = match.Groups["value"].Value;

Expand Down