Skip to content

Commit

Permalink
IDE0057 simplify substring (#11149)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardol committed Aug 15, 2023
1 parent 19e6463 commit 368c324
Show file tree
Hide file tree
Showing 65 changed files with 174 additions and 179 deletions.
2 changes: 1 addition & 1 deletion BugReporter/GitHubUrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public GitHubUrlBuilder(IErrorReportUrlBuilder errorReportUrlBuilder)
if (subject.Length > 69)
{
// if the subject is longer than 70 characters, trim it
subject = subject.Substring(0, 66) + "...";
subject = subject[..66] + "...";
}

string urlEncodedError = _errorReportUrlBuilder.Build(exception, exceptionInfo, environmentInfo, additionalInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static string AddPrefix(string word, AffixRule rule)

if (passCount == entry.ConditionCount)
{
string tempWord = word.Substring(entry.StripCharacters.Length);
string tempWord = word[entry.StripCharacters.Length..];
tempWord = entry.AddCharacters + tempWord;
return tempWord;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ public static string AddSuffix(string word, AffixRule rule)
if (passCount == entry.ConditionCount)
{
int tempLen = word.Length - entry.StripCharacters.Length;
string tempWord = word.Substring(0, tempLen);
string tempWord = word[..tempLen];
tempWord += entry.AddCharacters;
return tempWord;
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public static string RemovePrefix(string word, AffixEntry entry)
&& word.StartsWith(entry.AddCharacters))
{
// word with out affix
string tempWord = word.Substring(entry.AddCharacters.Length);
string tempWord = word[entry.AddCharacters.Length..];

// add back strip chars
tempWord = entry.StripCharacters + tempWord;
Expand Down Expand Up @@ -305,7 +305,7 @@ public static string RemoveSuffix(string word, AffixEntry entry)
&& word.EndsWith(entry.AddCharacters))
{
// word with out affix
string tempWord = word.Substring(0, tempLength);
string tempWord = word[..tempLength];

// add back strip chars
tempWord += entry.StripCharacters;
Expand Down
6 changes: 3 additions & 3 deletions Externals/NetSpell.SpellChecker/Dictionary/WordDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public string PhoneticCode(string word)
{
if (rule.ReplaceMode)
{
tempWord = rule.ReplaceString + tempWord.Substring(rule.ConditionCount - rule.ConsumeCount);
tempWord = rule.ReplaceString + tempWord[(rule.ConditionCount - rule.ConsumeCount)..];
}
else
{
Expand All @@ -591,7 +591,7 @@ public string PhoneticCode(string word)
code.Append(rule.ReplaceString);
}

tempWord = tempWord.Substring(rule.ConditionCount - rule.ConsumeCount);
tempWord = tempWord[(rule.ConditionCount - rule.ConsumeCount)..];
}

break;
Expand All @@ -602,7 +602,7 @@ public string PhoneticCode(string word)
// if no match consume one char
if (prevWord.Length == tempWord.Length)
{
tempWord = tempWord.Substring(1);
tempWord = tempWord[1..];
}
}

Expand Down
15 changes: 6 additions & 9 deletions Externals/NetSpell.SpellChecker/Spelling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,13 @@ private void ReplaceChars(List<Word> tempSuggestion)
for (int i = 0; i < replacementChars.Count; i++)
{
int split = replacementChars[i].IndexOf(' ');
string key = replacementChars[i].Substring(0, split);
string replacement = replacementChars[i].Substring(split + 1);
string key = replacementChars[i][..split];
string replacement = replacementChars[i][(split + 1)..];

int pos = CurrentWord.IndexOf(key, StringComparison.InvariantCulture);
while (pos > -1)
{
string tempWord = CurrentWord.Substring(0, pos);
tempWord += replacement;
tempWord += CurrentWord.Substring(pos + key.Length);
string tempWord = $"{CurrentWord[..pos]}{replacement}{CurrentWord[(pos + key.Length)..]}";

if (FindWord(ref tempWord))
{
Expand Down Expand Up @@ -403,8 +401,8 @@ private void TwoWords(List<Word> tempSuggestion)
{
for (int i = 1; i < CurrentWord.Length - 1; i++)
{
string firstWord = CurrentWord.Substring(0, i);
string secondWord = CurrentWord.Substring(i);
string firstWord = CurrentWord[..i];
string secondWord = CurrentWord[i..];

if (FindWord(ref firstWord) && FindWord(ref secondWord))
{
Expand Down Expand Up @@ -747,8 +745,7 @@ public void ReplaceWord()
// if first letter upper case, match case for replacement word
if (char.IsUpper(_words[replacedIndex].ToString(), 0))
{
_replacementWord = _replacementWord.Substring(0, 1).ToUpper(CultureInfo.CurrentUICulture)
+ _replacementWord.Substring(1);
_replacementWord = $"{char.ToUpper(_replacementWord[0], CultureInfo.CurrentUICulture)}{_replacementWord[1..]}";
}

_text.Insert(index, _replacementWord);
Expand Down
12 changes: 6 additions & 6 deletions GitCommands/Config/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ private void SetStringValue(string setting, string value)
{
var keyIndex = FindAndCheckKeyIndex(setting);

var configSectionName = setting.Substring(0, keyIndex);
var keyName = setting.Substring(keyIndex + 1);
var configSectionName = setting[..keyIndex];
var keyName = setting[(keyIndex + 1)..];

FindOrCreateConfigSection(configSectionName).SetValue(keyName, value);
}
Expand Down Expand Up @@ -149,8 +149,8 @@ public string GetValue(string setting, string defaultValue)

var keyIndex = FindAndCheckKeyIndex(setting);

var configSectionName = setting.Substring(0, keyIndex);
var keyName = setting.Substring(keyIndex + 1);
var configSectionName = setting[..keyIndex];
var keyName = setting[(keyIndex + 1)..];

var configSection = FindConfigSection(configSectionName);

Expand All @@ -171,8 +171,8 @@ public IReadOnlyList<string> GetValues(string setting)
{
var keyIndex = FindAndCheckKeyIndex(setting);

var configSectionName = setting.Substring(0, keyIndex);
var keyName = setting.Substring(keyIndex + 1);
var configSectionName = setting[..keyIndex];
var keyName = setting[(keyIndex + 1)..];

var configSection = FindConfigSection(configSectionName);

Expand Down
6 changes: 3 additions & 3 deletions GitCommands/Config/ConfigSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal ConfigSection(string name, bool forceCaseSensitive)
if (slashIndex != -1)
{
// [section "subsection"] case sensitive
SectionName = name.Substring(0, slashIndex).Trim();
SectionName = name[..slashIndex].Trim();
SubSection = name.Substring(slashIndex + 1, name.LastIndexOf('\"') - slashIndex - 1);
SubSectionCaseSensitive = true;
}
Expand All @@ -46,8 +46,8 @@ internal ConfigSection(string name, bool forceCaseSensitive)
throw new Exception("Invalid section name: " + name);
}

SectionName = name.Substring(0, subSectionIndex).Trim();
SubSection = name.Substring(subSectionIndex + 1).Trim();
SectionName = name[..subSectionIndex].Trim();
SubSection = name[(subSectionIndex + 1)..].Trim();
SubSectionCaseSensitive = false;
}

Expand Down
2 changes: 1 addition & 1 deletion GitCommands/CustomDiffMergeToolCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static IEnumerable<string> ParseCustomDiffMergeTool(string output, strin
// two tabs, then tool name, cmd (if split in 3) in second
// cmd is unreliable for diff and not needed but could be used for mergetool special handling
string[] delimit = { " ", ".cmd" };
var tool = l.Substring(2).Split(delimit, 2, StringSplitOptions.None);
var tool = l[2..].Split(delimit, 2, StringSplitOptions.None);
if (tool.Length == 0)
{
continue;
Expand Down
4 changes: 2 additions & 2 deletions GitCommands/Git/GitBranchNameNormaliser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ internal string Rule06(string branchName)
branchName = Regex.Replace(branchName, @"(\/{2,})", "/");
if (branchName.StartsWith("/"))
{
branchName = branchName.Substring(1);
branchName = branchName[1..];
}

if (branchName.EndsWith("/"))
{
branchName = branchName.Substring(0, branchName.Length - 1);
branchName = branchName[..^1];
}

return branchName;
Expand Down
8 changes: 4 additions & 4 deletions GitCommands/Git/GitDescribeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ public GitDescribeProvider(Func<IGitModule> getModule)
return (description, string.Empty);
}

string commitHash = description.Substring(commitHashPos + 2);
string commitHash = description[(commitHashPos + 2)..];
if (commitHash.Length == 0 || !revision.Equals(commitHash))
{
return (description, string.Empty);
}

description = description.Substring(0, commitHashPos);
description = description[..commitHashPos];
int commitCountPos = description.LastIndexOf("-", StringComparison.Ordinal);
if (commitCountPos == -1)
{
return (description, string.Empty);
}

string commitCount = description.Substring(commitCountPos + 1);
description = description.Substring(0, commitCountPos);
string commitCount = description[(commitCountPos + 1)..];
description = description[..commitCountPos];
return (description, commitCount);

IGitModule GetModule()
Expand Down
2 changes: 1 addition & 1 deletion GitCommands/Git/GitDirectoryResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public string Resolve(string repositoryPath)
var line = _fileSystem.File.ReadLines(gitPath).FirstOrDefault(l => l.StartsWith(gitdir));
if (line is not null)
{
string path = line.Substring(gitdir.Length).Trim().ToNativePath();
string path = line[gitdir.Length..].Trim().ToNativePath();
if (Path.IsPathRooted(path))
{
return path.EnsureTrailingPathSeparator();
Expand Down

0 comments on commit 368c324

Please sign in to comment.