Skip to content

Commit

Permalink
init: ported Trim_Suffix_{String,Unicode} primitive function
Browse files Browse the repository at this point in the history
init: ported Trim_Suffix_{String,Unicode} primitive function

Since a number of level 1 Hestia libraries use string functions,
we have to port its primitive ones into HestiaKERNEL library package.
Hence, let's do this.

This patch ports Get_Suffix_{String,Unicode} primitive function
into HestiaKERNEL library 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 7, 2024
1 parent 50e46b8 commit ded34f0
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 9 deletions.
53 changes: 53 additions & 0 deletions init/services/HestiaKERNEL/String/Trim_Suffix_String.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2024 (Holloway) Chew, Kean Ho <[email protected]>
#
#
# Licensed under (Holloway) Chew, Kean Ho’s Liberal License (the "License").
# You must comply with the license to use the content. Get the License at:
#
# https://doi.org/10.5281/zenodo.13770769
#
# You MUST ensure any interaction with the content STRICTLY COMPLIES with
# the permissions and limitations set forth in the license.
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\To_String_From_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Trim_Suffix_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\To_Unicode_From_String.ps1"




function HestiaKERNEL-Trim-Suffix-String {
param (
[string]$___input,
[string]$___suffix
)


# 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
}

$___content = HestiaKERNEL-Trim-Suffix-Unicode $___content $___chars
if ($___content.Length -eq 0) {
return $___input
}


# report status
return HestiaKERNEL-To-String-From-Unicode $___content
}
57 changes: 57 additions & 0 deletions init/services/HestiaKERNEL/String/Trim_Suffix_String.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh
# Copyright 2024 (Holloway) Chew, Kean Ho <[email protected]>
#
#
# Licensed under (Holloway) Chew, Kean Ho’s Liberal License (the "License").
# You must comply with the license to use the content. Get the License at:
#
# https://doi.org/10.5281/zenodo.13770769
#
# 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"




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


# 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"


# report status
return $HestiaKERNEL_ERROR_OK
}
7 changes: 3 additions & 4 deletions init/services/HestiaKERNEL/Unicode/Trim_Prefix_Unicode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ function HestiaKERNEL-Trim-Prefix-Unicode {


# execute
for ($i = 0; $i -le $___prefix_unicode.Length - 1; $i++) {
$___index = $___prefix_unicode.Length - 1
for ($i = 0; $i -le $___index; $i++) {
# get current character
$___current = ""
$___current = $___content_unicode[$i]


Expand All @@ -50,7 +50,6 @@ function HestiaKERNEL-Trim-Prefix-Unicode {
}
}


# report status
return [uint32[]]$___content_unicode[$i..($___content_unicode.Length - 1)]
return [uint32[]]$___content_unicode[$___index..($___content_unicode.Length - 1)]
}
7 changes: 2 additions & 5 deletions init/services/HestiaKERNEL/Unicode/Trim_Prefix_Unicode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@


HestiaKERNEL_Trim_Prefix_Unicode() {
#___input_content_unicode="$1"
#___input_prefix_unicode="$2"
#___content_unicode="$1"
#___prefix_unicode="$2"


# validate input
Expand All @@ -33,9 +33,6 @@ HestiaKERNEL_Trim_Prefix_Unicode() {


# execute
## IMPORTANT NOTICE
## POSIX Shell's UNIX regex cannot recognize anything outside Latin-1
## script. Therefore, manual algorithmic handling is required.
___content_unicode="$1"
___prefix_unicode="$2"
while [ ! "$___prefix_unicode" = "" ]; do
Expand Down
57 changes: 57 additions & 0 deletions init/services/HestiaKERNEL/Unicode/Trim_Suffix_Unicode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2024 (Holloway) Chew, Kean Ho <[email protected]>
#
#
# Licensed under (Holloway) Chew, Kean Ho’s Liberal License (the "License").
# You must comply with the license to use the content. Get the License at:
#
# https://doi.org/10.5281/zenodo.13770769
#
# You MUST ensure any interaction with the content STRICTLY COMPLIES with
# the permissions and limitations set forth in the license.
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Is_Unicode.ps1"




function HestiaKERNEL-Trim-Suffix-Unicode {
param (
[uint32[]]$___content_unicode,
[uint32[]]$___suffix_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})
) {
return $___content_unicode
}

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


# execute
$___index = $___suffix_unicode.Length - 1
for ($i = $___index; $i -ge 0; $i--) {
# get current character
$___current = $___content_unicode[$i]


# get target character
$___target = $___suffix_unicode[$i]


# bail if mismatched
if ($___current -ne $___target) {
return $___content_unicode
}
}
$___index = ($___content_unicode.Length - 1) - $___index


# report status
return [uint32[]]$___content_unicode[0..$___index]
}
66 changes: 66 additions & 0 deletions init/services/HestiaKERNEL/Unicode/Trim_Suffix_Unicode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2024 (Holloway) Chew, Kean Ho <[email protected]>
#
#
# Licensed under (Holloway) Chew, Kean Ho’s Liberal License (the "License").
# You must comply with the license to use the content. Get the License at:
#
# https://doi.org/10.5281/zenodo.13770769
#
# 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/Is_Unicode.sh"




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


# validate input
if [ "$(HestiaKERNEL_Is_Unicode "$1")" -ne $HestiaKERNEL_ERROR_OK ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_ENTITY_EMPTY
fi

if [ "$(HestiaKERNEL_Is_Unicode "$2")" -ne $HestiaKERNEL_ERROR_OK ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_DATA_EMPTY
fi


# execute
___content_unicode="$1"
___suffix_unicode="$2"
while [ ! "$___suffix_unicode" = "" ]; do
# get current character
___current="${___content_unicode##*, }"
___content_unicode="${___content_unicode%"$___current"}"
if [ "${___content_unicode#"${___content_unicode%?}"}" = " " ]; then
___content_unicode="${___content_unicode%, }"
fi


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


# bail if mismatched
if [ "$___current" != "$___target" ] ||
([ "$___content_unicode" = "" ] && [ ! "$___suffix_unicode" = "" ]); then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_OK
fi
done


# report status
printf -- "%s" "${___content_unicode%, }"
return $HestiaKERNEL_ERROR_OK
}
8 changes: 8 additions & 0 deletions init/services/HestiaKERNEL/Vanilla.sh.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ echo \" <<'RUN_AS_POWERSHELL' >/dev/null # " | Out-Null
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\To_Titlecase_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\To_Uppercase_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Trim_Left_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Trim_Prefix_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Trim_Right_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Trim_Suffix_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Get_First_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Get_Last_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Is_Empty_Unicode.ps1"
Expand All @@ -68,7 +70,9 @@ echo \" <<'RUN_AS_POWERSHELL' >/dev/null # " | Out-Null
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\To_UTF16_From_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\To_UTF32_From_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Trim_Left_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Trim_Prefix_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Trim_Right_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Trim_Suffix_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode\Unicode.ps1"
################################################################################
# Windows POWERSHELL Codes #
Expand Down Expand Up @@ -100,7 +104,9 @@ RUN_AS_POWERSHELL
. "${LIBS_HESTIA}/HestiaKERNEL/String/To_Titlecase_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/String/To_Uppercase_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/String/Trim_Left_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/String/Trim_Prefix_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/String/Trim_Right_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/String/Trim_Suffix_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Get_First_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Get_Last_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Is_Empty_Unicode.sh"
Expand All @@ -119,7 +125,9 @@ RUN_AS_POWERSHELL
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/To_UTF16_From_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/To_UTF32_From_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Trim_Left_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Trim_Prefix_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Trim_Right_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Trim_Suffix_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode/Unicode.sh"
################################################################################
# Unix Main Codes #
Expand Down
7 changes: 7 additions & 0 deletions init/start.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,16 @@ ${env:LIBS_HESTIA} = "${env:LIBS_UPSCALER}\services"
### TEST ZONE
. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Trim_Prefix_String.ps1"
Write-Host "$(HestiaKERNEL-Trim-Prefix-String "e你feeeff你你aerg aegE你F" '')"
Write-Host "$(HestiaKERNEL-Trim-Prefix-String "e你feeeff你你aerg aegE你F" "e你feeeff你你aerg aegE你FX")"
Write-Host "$(HestiaKERNEL-Trim-Prefix-String "e你feeeff你你aerg aegE你F" "e你a")"
Write-Host "$(HestiaKERNEL-Trim-Prefix-String "e你feeeff你你aerg aegE你F" "e你f")"

. "${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你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")"

. "${env:LIBS_HESTIA}\HestiaKERNEL\String\Get_Length_String.ps1"
Write-Host "$(HestiaKERNEL-Get-Length-String '')"
Write-Host "$(HestiaKERNEL-Get-Length-String "f")"
Expand Down
7 changes: 7 additions & 0 deletions init/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,16 @@ LIBS_HESTIA="${LIBS_UPSCALER}/services"
### TEST ZONE
. "${LIBS_HESTIA}/HestiaKERNEL/String/Trim_Prefix_String.sh"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Prefix_String "e你feeeff你你aerg aegE你F" "")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Prefix_String "e你feeeff你你aerg aegE你F" "e你feeeff你你aerg aegE你FX")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Prefix_String "e你feeeff你你aerg aegE你F" "e你a")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Prefix_String "e你feeeff你你aerg aegE你F" "e你f")"

. "${LIBS_HESTIA}/HestiaKERNEL/String/Trim_Suffix_String.sh"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Suffix_String "e你feeeff你你aerg aegE你F" "")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Suffix_String "e你feeeff你你aerg aegE你F" "e你feeeff你你aerg aegE你FX")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Suffix_String "e你feeeff你你aerg aegE你F" "E你A")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Suffix_String "e你feeeff你你aerg aegE你F" "E你F")"

. "${LIBS_HESTIA}/HestiaKERNEL/String/Get_Length_String.sh"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Get_Length_String "")"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Get_Length_String "f")"
Expand Down

0 comments on commit ded34f0

Please sign in to comment.