Skip to content

Commit

Permalink
Switch from string.Format to interpolated strings to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthysell committed Sep 9, 2021
1 parent 36e698a commit 5f4fe71
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Improved PukuiElbert source parsing and cleanup
* New HawDict.Test test library with StringUtils tests
* Process dictionaries in parallel to speed up result
* Switch from string.Format to interpolated strings to improve performance

## v0.12 ##

Expand Down
2 changes: 1 addition & 1 deletion src/HawDict/Input/HtmlInputDict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public HtmlInputDict(string id, TranslationType translationType, LogLine logLine

protected override void GetRawDataFromSource()
{
string htmlFile = Path.Combine(DictDir, string.Format("{0}.{1}.html.tmp", ID, TranslationType.ToString()));
string htmlFile = Path.Combine(DictDir, $"{ID}.{TranslationType}.html.tmp");

string html = "";

Expand Down
2 changes: 1 addition & 1 deletion src/HawDict/Input/InputDictBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void Process(string rootDir)

Log("Save start.");

string cleanFile = Path.Combine(DictDir, string.Format("{0}.{1}.clean.txt", ID, TranslationType.ToString()));
string cleanFile = Path.Combine(DictDir, $"{ID}.{TranslationType}.clean.txt");
SaveCleanFile(cleanFile);

SaveOutputDict<XdxfDictionary>();
Expand Down
4 changes: 2 additions & 2 deletions src/HawDict/Input/MamakaKaiaoInputDict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class MamakaKaiaoInputDict : HtmlInputDict
{
public MamakaKaiaoInputDict(TranslationType translationType, LogLine logLine) : base("MamakaKaiao", translationType, logLine)
{
ShortTitle = string.Format("Māmaka Kaiao ({0})", TranslationType == TranslationType.HawToEng ? "HAW-ENG" : "ENG-HAW");
LongTitle = string.Format("Māmaka Kaiao: A Modern Hawaiian Vocabulary ({0})", TranslationType == TranslationType.HawToEng ? "Hawaiian-English" : "English-Hawaiian");
ShortTitle = $"Māmaka Kaiao ({(TranslationType == TranslationType.HawToEng ? "HAW-ENG" : "ENG-HAW")})";
LongTitle = $"Māmaka Kaiao: A Modern Hawaiian Vocabulary ({(TranslationType == TranslationType.HawToEng ? "Hawaiian-English" : "English-Hawaiian")})";
Description = "A compilation of Hawaiian words that have been created, collected, and approved by the Hawaiian Lexicon Committee from 1987 through 2000. Copyright (c) 2003 ʻAha Pūnana Leo and Hale Kuamoʻo, College of Hawaiian Language, University of Hawaiʻi at Hilo (ISBN 978-0824828035)";

Authors.AddRange(new string[] { "Kōmike Huaʻōlelo", "Hale Kuamoʻo", "ʻAha Pūnana Leo" });
Expand Down
4 changes: 2 additions & 2 deletions src/HawDict/Input/PukuiElbertInputDict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class PukuiElbertInputDict : HtmlInputDict

public PukuiElbertInputDict(TranslationType translationType, LogLine logLine) : base("PukuiElbert", translationType, logLine)
{
ShortTitle = string.Format("Hawaiian Dictionary ({0})", TranslationType == TranslationType.HawToEng ? "HAW-ENG" : "ENG-HAW");
LongTitle = string.Format("Hawaiian Dictionary, Revised and Enlarged Edition ({0})", TranslationType == TranslationType.HawToEng ? "Hawaiian-English" : "English-Hawaiian");
ShortTitle = $"Hawaiian Dictionary ({(TranslationType == TranslationType.HawToEng ? "HAW-ENG" : "ENG-HAW")})";
LongTitle = $"Hawaiian Dictionary, Revised and Enlarged Edition ({(TranslationType == TranslationType.HawToEng ? "Hawaiian-English" : "English-Hawaiian")})";
Description = "The reference standard Hawaiian-English and English-Hawaiian dictionary. Copyright (c) 1986 University of Hawaii Press (ISBN 978-0824807030)";

Authors.AddRange(new string[] { "Mary Kawena Pūkuʻi", "Samuel H. Elbert" });
Expand Down
38 changes: 19 additions & 19 deletions src/HawDict/Output/OutputArticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private string GetXdxfValue(bool keepDefinitionNumbers = false)
if (value.StartsWith("<gr>"))
{
int grammarEndIndex = value.IndexOf("</gr>") + 5;
grammar = string.Format("{0}", value.Substring(0, grammarEndIndex));
grammar = $"{value.Substring(0, grammarEndIndex)}";
value = value.Substring(grammarEndIndex + 1);
}

Expand All @@ -140,11 +140,11 @@ private string GetXdxfValue(bool keepDefinitionNumbers = false)
if (definitions.Count() > 1)
{
value = string.Join("</deftext></def><def><deftext>", definitions);
value = string.Format("<def>{0}<def><deftext>{1}</deftext></def></def>", grammar, value);
value = $"<def>{grammar}<def><deftext>{value}</deftext></def></def>";
}
else
{
value = string.Format("<def>{0}<deftext>{1}</deftext></def>", grammar, value);
value = $"<def>{grammar}<deftext>{value}</deftext></def>";
}

return value;
Expand All @@ -154,8 +154,8 @@ private static IEnumerable<string> GetDefinitions(string value, bool keepDefinit
{
int nextFoundIndex = -1;

string numStr = string.Format("{0}. ", num);
string nextNumStr = string.Format(" {0}. ", num + 1);
string numStr = $"{num}. ";
string nextNumStr = $" {num + 1}. ";

if (value.StartsWith(numStr) && (nextFoundIndex = value.IndexOf(nextNumStr)) > 0)
{
Expand Down Expand Up @@ -195,38 +195,38 @@ private static IEnumerable<string> GetDefinitions(string value, bool keepDefinit

private static string AddXdxfAbbreviationTags(string value, string abbreviation, bool grammar)
{
value = value.Replace(string.Format(" {0} ", abbreviation), string.Format(" <abbr>{0}</abbr> ", abbreviation));
value = value.Replace(string.Format("({0} ", abbreviation), string.Format("(<abbr>{0}</abbr> ", abbreviation));
value = value.Replace(string.Format(" {0})", abbreviation), string.Format(" <abbr>{0}</abbr>)", abbreviation));
value = value.Replace(string.Format("({0})", abbreviation), string.Format("(<abbr>{0}</abbr>)", abbreviation));
value = value.Replace($" {abbreviation} ", $" <abbr>{abbreviation}</abbr> ");
value = value.Replace($"({abbreviation} ", $"(<abbr>{abbreviation}</abbr> ");
value = value.Replace($" {abbreviation})", $" <abbr>{abbreviation}</abbr>)");
value = value.Replace($"({abbreviation})", $"(<abbr>{abbreviation}</abbr>)");

value = value.Replace(string.Format("{0}.", abbreviation), string.Format("<abbr>{0}</abbr>.", abbreviation));
value = value.Replace($"{abbreviation}.", $"<abbr>{abbreviation}</abbr>.");

value = value.Replace(string.Format("{0};", abbreviation), string.Format("<abbr>{0}</abbr>;", abbreviation));
value = value.Replace($"{abbreviation};", $"<abbr>{abbreviation}</abbr>;");

value = value.Replace(string.Format("{0},", abbreviation), string.Format("<abbr>{0}</abbr>,", abbreviation));
value = value.Replace(string.Format("({0},", abbreviation), string.Format("(<abbr>{0}</abbr>,", abbreviation));
value = value.Replace($"{abbreviation},", $"<abbr>{abbreviation}</abbr>,");
value = value.Replace($"({abbreviation},", $"(<abbr>{abbreviation}</abbr>,");

value = value.Replace(string.Format("{0}/", abbreviation), string.Format("<abbr>{0}</abbr>/", abbreviation));
value = value.Replace(string.Format("/{0}", abbreviation), string.Format("/<abbr>{0}</abbr>", abbreviation));
value = value.Replace($"{abbreviation}/", $"<abbr>{abbreviation}</abbr>/");
value = value.Replace($"/{abbreviation}", $"/<abbr>{abbreviation}</abbr>");

value = value.Replace(string.Format("—{0}", abbreviation), string.Format("—<abbr>{0}</abbr> ", abbreviation));
value = value.Replace($"—{abbreviation}", $"—<abbr>{abbreviation}</abbr> ");

if (value.StartsWith(abbreviation + " "))
{
if (grammar)
{
value = string.Format("<gr><abbr>{0}</abbr></gr>{1}", abbreviation, value.Substring(abbreviation.Length));
value = $"<gr><abbr>{abbreviation}</abbr></gr>{value.Substring(abbreviation.Length)}";
}
else
{
value = string.Format("<abbr>{0}</abbr>{1}", abbreviation, value.Substring(abbreviation.Length));
value = $"<abbr>{abbreviation}</abbr>{value.Substring(abbreviation.Length)}";
}
}

if (value.EndsWith(" " + abbreviation))
{
value = string.Format("{0}<abbr>{1}</abbr>", value.Substring(0, value.Length - abbreviation.Length), abbreviation);
value = $"{value.Substring(0, value.Length - abbreviation.Length)}<abbr>{abbreviation}</abbr>";
}

return value;
Expand Down
8 changes: 4 additions & 4 deletions src/HawDict/Output/StarDictDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public override void Save(string dictDir)

private void SaveDataFiles(string dictDir, out long idxFileSize, out int synWordCount)
{
string dictFile = Path.Combine(dictDir, string.Format("{0}.{1}.StarDict.dict", ID, TranslationType.ToString()));
string idxFile = Path.Combine(dictDir, string.Format("{0}.{1}.StarDict.idx", ID, TranslationType.ToString()));
string synFile = Path.Combine(dictDir, string.Format("{0}.{1}.StarDict.syn", ID, TranslationType.ToString()));
string dictFile = Path.Combine(dictDir, $"{ID}.{TranslationType}.StarDict.dict");
string idxFile = Path.Combine(dictDir, $"{ID}.{TranslationType}.StarDict.idx");
string synFile = Path.Combine(dictDir, $"{ID}.{TranslationType}.StarDict.syn");

BinaryWriter dictWriter = new BinaryWriter(new FileStream(dictFile, FileMode.Create), Encoding.UTF8);
BinaryWriter idxWriter = new BinaryWriter(new FileStream(idxFile, FileMode.Create), Encoding.UTF8);
Expand Down Expand Up @@ -96,7 +96,7 @@ private void SaveDataFiles(string dictDir, out long idxFileSize, out int synWord

private void SaveIfoFile(string dictDir, long idxFileSize, int synWordCount)
{
string ifoFile = Path.Combine(dictDir, string.Format("{0}.{1}.StarDict.ifo", ID, TranslationType.ToString()));
string ifoFile = Path.Combine(dictDir, $"{ID}.{TranslationType}.StarDict.ifo");

using (BinaryWriter ifoWriter = new BinaryWriter(new FileStream(ifoFile, FileMode.Create), Encoding.UTF8))
{
Expand Down
2 changes: 1 addition & 1 deletion src/HawDict/Output/XdxfDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override void Save(string dictDir)
throw new ArgumentNullException(nameof(dictDir));
}

string xdxfFile = Path.Combine(dictDir, string.Format("{0}.{1}.dict.xdxf", ID, TranslationType.ToString()));
string xdxfFile = Path.Combine(dictDir, $"{ID}.{TranslationType}.dict.xdxf");

using (FileStream fs = new FileStream(xdxfFile, FileMode.Create))
{
Expand Down
2 changes: 1 addition & 1 deletion src/HawDict/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static string WrapInTag(string s, string tag)
throw new ArgumentNullException(nameof(tag));
}

return string.Format("<{0}>{1}</{0}>", tag, s);
return $"<{tag}>{s}</{tag}>";
}

public static string WrapInTag(string s, string target, string tag)
Expand Down

0 comments on commit 5f4fe71

Please sign in to comment.