Skip to content

Commit

Permalink
Small optimizations from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcouch-sil committed Jun 3, 2024
1 parent 506a70b commit 61ea65a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 79 deletions.
9 changes: 4 additions & 5 deletions c-sharp/Projects/ParatextProjectDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ LocalParatextProjects paratextProjects

protected override Task StartDataProvider()
{
var shouldIncludePT9ProjectsOnWindows = false;
bool? shouldIncludePT9ProjectsOnWindows = false;
if (OperatingSystem.IsWindows())
{
var shouldIncludePT9ProjectsElement = SettingsService.GetSettingValue<bool>(PapiClient, Settings.INCLUDE_MY_PARATEXT_9_PROJECTS);
if (!shouldIncludePT9ProjectsElement.HasValue)
shouldIncludePT9ProjectsOnWindows = SettingsService.GetSettingValue<bool>(PapiClient, Settings.INCLUDE_MY_PARATEXT_9_PROJECTS);
if (!shouldIncludePT9ProjectsOnWindows.HasValue)
throw new Exception($"Setting {Settings.INCLUDE_MY_PARATEXT_9_PROJECTS} was null!");
shouldIncludePT9ProjectsOnWindows = shouldIncludePT9ProjectsElement.Value;
}
_paratextProjects.Initialize(shouldIncludePT9ProjectsOnWindows);
_paratextProjects.Initialize(shouldIncludePT9ProjectsOnWindows.Value);
return Task.CompletedTask;
}

Expand Down
9 changes: 4 additions & 5 deletions c-sharp/Projects/ParatextProjectDataProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ LocalParatextProjects paratextProjects

protected override Task StartFactory()
{
var shouldIncludePT9ProjectsOnWindows = false;
bool? shouldIncludePT9ProjectsOnWindows = false;
if (OperatingSystem.IsWindows())
{
var shouldIncludePT9ProjectsElement = SettingsService.GetSettingValue<bool>(PapiClient, Settings.INCLUDE_MY_PARATEXT_9_PROJECTS);
if (!shouldIncludePT9ProjectsElement.HasValue)
shouldIncludePT9ProjectsOnWindows = SettingsService.GetSettingValue<bool>(PapiClient, Settings.INCLUDE_MY_PARATEXT_9_PROJECTS);
if (!shouldIncludePT9ProjectsOnWindows.HasValue)
throw new Exception($"Setting {Settings.INCLUDE_MY_PARATEXT_9_PROJECTS} was null!");
shouldIncludePT9ProjectsOnWindows = shouldIncludePT9ProjectsElement.Value;
}
_paratextProjects.Initialize(shouldIncludePT9ProjectsOnWindows);
_paratextProjects.Initialize(shouldIncludePT9ProjectsOnWindows.Value);
return Task.CompletedTask;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/platform-bible-utils/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/platform-bible-utils/dist/index.cjs.map

Large diffs are not rendered by default.

73 changes: 36 additions & 37 deletions lib/platform-bible-utils/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/platform-bible-utils/dist/index.js.map

Large diffs are not rendered by default.

54 changes: 25 additions & 29 deletions lib/platform-bible-utils/src/string-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,33 @@ export function formatReplacementString(str: string, replacers: { [key: string]:
while (i < stringLength(updatedStr)) {
switch (charAt(updatedStr, i)) {
case '{':
if (charAt(updatedStr, i) === '{') {
if (charAt(updatedStr, i - 1) !== '\\') {
// This character is an un-escaped open curly brace. Try to match and replace
const closeCurlyBraceIndex = indexOfClosestClosingCurlyBrace(updatedStr, i, false);
if (closeCurlyBraceIndex >= 0) {
// We have matching open and close indices. Try to replace the contents
const replacerKey = substring(updatedStr, i + 1, closeCurlyBraceIndex);
// Replace with the replacer string or just remove the curly braces
const replacerString =
replacerKey in replacers ? replacers[replacerKey] : replacerKey;

updatedStr = `${substring(updatedStr, 0, i)}${replacerString}${substring(updatedStr, closeCurlyBraceIndex + 1)}`;
// Put our index at the closing brace adjusted for the new string length minus two
// because we are removing the curly braces
// Ex: "stuff {and} things"
// Replacer for and: n'
// closeCurlyBraceIndex is 10
// "stuff n' things"
// i = 10 + 2 - 3 - 2 = 7
i =
closeCurlyBraceIndex + stringLength(replacerString) - stringLength(replacerKey) - 2;
} else {
// This is an unexpected un-escaped open curly brace with no matching closing curly
// brace. Just ignore, I guess
}
if (charAt(updatedStr, i - 1) !== '\\') {
// This character is an un-escaped open curly brace. Try to match and replace
const closeCurlyBraceIndex = indexOfClosestClosingCurlyBrace(updatedStr, i, false);
if (closeCurlyBraceIndex >= 0) {
// We have matching open and close indices. Try to replace the contents
const replacerKey = substring(updatedStr, i + 1, closeCurlyBraceIndex);
// Replace with the replacer string or just remove the curly braces
const replacerString = replacerKey in replacers ? replacers[replacerKey] : replacerKey;

updatedStr = `${substring(updatedStr, 0, i)}${replacerString}${substring(updatedStr, closeCurlyBraceIndex + 1)}`;
// Put our index at the closing brace adjusted for the new string length minus two
// because we are removing the curly braces
// Ex: "stuff {and} things"
// Replacer for and: n'
// closeCurlyBraceIndex is 10
// "stuff n' things"
// i = 10 + 2 - 3 - 2 = 7
i = closeCurlyBraceIndex + stringLength(replacerString) - stringLength(replacerKey) - 2;
} else {
// This character is an escaped open curly brace. Remove the escape
updatedStr = `${substring(updatedStr, 0, i - 1)}${substring(updatedStr, i)}`;
// Adjust our index because we removed the escape
i -= 1;
// This is an unexpected un-escaped open curly brace with no matching closing curly
// brace. Just ignore, I guess
}
} else {
// This character is an escaped open curly brace. Remove the escape
updatedStr = `${substring(updatedStr, 0, i - 1)}${substring(updatedStr, i)}`;
// Adjust our index because we removed the escape
i -= 1;
}
break;
case '}':
Expand Down

0 comments on commit 61ea65a

Please sign in to comment.