Skip to content

Commit

Permalink
init: backported changes to Trim_Suffix_* primitive functions
Browse files Browse the repository at this point in the history
There were some new optimization discovered when implementing
Index_Right_{String,Unicode} functions. Hence, let's backport those
changes back to its predecessor Trim_Suffix_{String,Unicode}.

This patch backports changes to Trim_Suffix_{String,Unicode}
primitive functions in init/ directory.

Co-authored-by: Shuralyov, Jean <[email protected]>
Co-authored-by: Galyna, Cory <[email protected]>
Co-authored-by: (Holloway) Chew, Kean Ho <[email protected]>
Signed-off-by: (Holloway) Chew, Kean Ho <[email protected]>
  • Loading branch information
4 people committed Nov 11, 2024
1 parent 6251a82 commit 5f11609
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 65 deletions.
1 change: 0 additions & 1 deletion init/services/HestiaKERNEL/String/Index_Right_String.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#
# You MUST ensure any interaction with the content STRICTLY COMPLIES with
# the permissions and limitations set forth in the license.
. "${LIBS_HESTIA}/HestiaKERNEL/Errors/Error_Codes.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Index_Right_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/To_Unicode_From_String.sh"

Expand Down
21 changes: 2 additions & 19 deletions init/services/HestiaKERNEL/String/Trim_Suffix_String.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,13 @@
function HestiaKERNEL-Trim-Suffix-String {
param (
[string]$___input,
[string]$___suffix
[string]$___target
)


# validate input
if (
($___input -eq "") -or
($___suffix -eq "")
) {
return $___input
}


# execute
$___content = HestiaKERNEL-To-Unicode-From-String $___input
if ($___content.Length -eq 0) {
return $___input
}

$___chars = HestiaKERNEL-To-Unicode-From-String $___suffix
if ($___chars.Length -eq 0) {
return $___input
}

$___chars = HestiaKERNEL-To-Unicode-From-String $___target
$___content = HestiaKERNEL-Trim-Suffix-Unicode $___content $___chars


Expand Down
30 changes: 3 additions & 27 deletions init/services/HestiaKERNEL/String/Trim_Suffix_String.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#
# You MUST ensure any interaction with the content STRICTLY COMPLIES with
# the permissions and limitations set forth in the license.
. "${LIBS_HESTIA}/HestiaKERNEL/Errors/Error_Codes.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/String/To_String_From_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Trim_Suffix_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/To_Unicode_From_String.sh"
Expand All @@ -19,39 +18,16 @@

HestiaKERNEL_Trim_Suffix_String() {
#___input="$1"
#___suffix="$2"


# validate input
if [ "$1" = "" ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_ENTITY_EMPTY
fi

if [ "$2" = "" ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_DATA_EMPTY
fi
#___target="$2"


# execute
___content="$(HestiaKERNEL_To_Unicode_From_String "$1")"
if [ "$___content" = "" ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_DATA_INVALID
fi

___chars="$(HestiaKERNEL_To_Unicode_From_String "$2")"
if [ "$___chars" = "" ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_DATA_INVALID
fi

___content="$(HestiaKERNEL_Trim_Suffix_Unicode "$___content" "$___chars")"
___content="$(HestiaKERNEL_To_String_From_Unicode "$___content")"
printf -- "%s" "$___content"
printf -- "%s" "$(HestiaKERNEL_To_String_From_Unicode "$___content")"


# report status
return $HestiaKERNEL_ERROR_OK
return $?
}
11 changes: 5 additions & 6 deletions init/services/HestiaKERNEL/Unicode/Trim_Suffix_Unicode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@
function HestiaKERNEL-Trim-Suffix-Unicode {
param (
[uint32[]]$___content_unicode,
[uint32[]]$___suffix_unicode
[uint32[]]$___target_unicode
)


# validate input
if (
($(HestiaKERNEL-Is-Unicode $___content_unicode) -ne ${env:HestiaKERNEL_ERROR_OK}) -or
($(HestiaKERNEL-Is-Unicode $___suffix_unicode) -ne ${env:HestiaKERNEL_ERROR_OK})
($(HestiaKERNEL-Is-Unicode $___target_unicode) -ne ${env:HestiaKERNEL_ERROR_OK})
) {
return $___content_unicode
}

if ($___suffix_unicode.Length -gt $___content_unicode.Length) {
if ($___target_unicode.Length -gt $___content_unicode.Length) {
return $___content_unicode
}


# execute
$___index = $___content_unicode.Length - 1
for ($i = $___suffix_unicode.Length - 1; $i -ge 0; $i--) {
for ($i = $___target_unicode.Length - 1; $i -ge 0; $i--) {
# bail if mismatched
if ($___content_unicode[$___index] -ne $___suffix_unicode[$i]) {
if ($___content_unicode[$___index] -ne $___target_unicode[$i]) {
return $___content_unicode
}

Expand All @@ -47,7 +47,6 @@ function HestiaKERNEL-Trim-Suffix-Unicode {
if ($___index -le 0) {
return [uint32[]]@()
}
$___index += 1


# report status
Expand Down
16 changes: 8 additions & 8 deletions init/services/HestiaKERNEL/Unicode/Trim_Suffix_Unicode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

HestiaKERNEL_Trim_Suffix_Unicode() {
#___content_unicode="$1"
#___charset_unicode="$2"
#___target_unicode="$2"


# validate input
Expand All @@ -33,8 +33,8 @@ HestiaKERNEL_Trim_Suffix_Unicode() {

# execute
___content_unicode="$1"
___suffix_unicode="$2"
while [ ! "$___suffix_unicode" = "" ]; do
___target_unicode="$2"
while [ ! "$___target_unicode" = "" ]; do
# get current character
___current="${___content_unicode##*, }"
___content_unicode="${___content_unicode%"$___current"}"
Expand All @@ -44,16 +44,16 @@ HestiaKERNEL_Trim_Suffix_Unicode() {


# get target character
___target="${___suffix_unicode##*, }"
___suffix_unicode="${___suffix_unicode%"$___target"}"
if [ "${___suffix_unicode#"${___suffix_unicode%?}"}" = " " ]; then
___suffix_unicode="${___suffix_unicode%, }"
___target="${___target_unicode##*, }"
___target_unicode="${___target_unicode%"$___target"}"
if [ "${___target_unicode#"${___target_unicode%?}"}" = " " ]; then
___target_unicode="${___target_unicode%, }"
fi


# bail if mismatched
if [ "$___current" != "$___target" ] ||
([ "$___content_unicode" = "" ] && [ ! "$___suffix_unicode" = "" ]); then
([ "$___content_unicode" = "" ] && [ ! "$___target_unicode" = "" ]); then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_OK
fi
Expand Down
8 changes: 4 additions & 4 deletions init/start.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ Write-Host "----"
Write-Host "---- Trim_Suffix_String ----"
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Trim_Suffix_String.ps1"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" '')|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String '' "e你F")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String '' "e你a")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String '' "E你F")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String '' "E你A")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" "e你feeeff你你aerg aegE你F")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" "e你feeeff你你aerg aegE你FX")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" "e你a")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" "e你F")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" "E你A")|"
Write-Host "|$(HestiaKERNEL-Trim-Suffix-String "e你feeeff你你aerg aegE你F" "E你F")|"
Write-Host "----"

Write-Host "---- Get_Length_String ----"
Expand Down

0 comments on commit 5f11609

Please sign in to comment.