Skip to content

Commit

Permalink
init: Trim_Right_{String,Unicode} primitive function
Browse files Browse the repository at this point in the history
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 Trim_Right_{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 5, 2024
1 parent 00a374e commit 0250e86
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 7 deletions.
10 changes: 8 additions & 2 deletions init/services/HestiaKERNEL/Trim_Left_Unicode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ function HestiaKERNEL-Trim-Left-Unicode {
($(HestiaKERNEL-Is-Unicode $___content_unicode) -ne ${env:HestiaKERNEL_ERROR_OK}) -or
($(HestiaKERNEL-Is-Unicode $___charset_unicode) -ne ${env:HestiaKERNEL_ERROR_OK})
) {
return $___unicode
return $___content_unicode
}


# execute
[System.Collections.Generic.List[uint32]]$___converted = @()
$___is_scanning = 0
:scan_unicode foreach ($___current in $___content_unicode) {
:scan_unicode for ($i = 0; $i -le $___content_unicode.Length - 1; $i++) {
# get current character
$___current = $___content_unicode[$i]


# it's already mismatched so prefix the remaining values
if ($___is_scanning -ne 0) {
$null = $___converted.Add($___current)
continue scan_unicode
Expand All @@ -46,6 +51,7 @@ function HestiaKERNEL-Trim-Left-Unicode {
}
}


# It's an mismatched
$___is_scanning = 1
$null = $___converted.Add($___current)
Expand Down
10 changes: 5 additions & 5 deletions init/services/HestiaKERNEL/Trim_Left_Unicode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ HestiaKERNEL_Trim_Left_Unicode() {

# get char from charset character
___charset_list="$___charset_unicode"
___mismatched=0
while [ ! "$___charset_list" = "" ]; do
___char="${___charset_list%%, *}"
___charset_list="${___charset_list#"$___char"}"
Expand All @@ -59,18 +60,17 @@ HestiaKERNEL_Trim_Left_Unicode() {

if [ "$___current" = "$___char" ]; then
___charset_list=""
___mismatched=1
break # exit early from O(m^2) timing ASAP
fi
done

if [ ! "$___current" = "$___char" ]; then
# It's an mismatched

# It's a mismatch - append the rest and bail out
if [ $___mismatched -eq 0 ]; then
___converted="${___current}, ${___content_unicode}"
break
fi


# it's a match - do nothing and continue to next character
done


Expand Down
53 changes: 53 additions & 0 deletions init/services/HestiaKERNEL/Trim_Right_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\Trim_Right_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\To_Unicode_From_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\To_String_From_Unicode.ps1"




function HestiaKERNEL-Trim-Right-String {
param (
[string]$___input,
[string]$___charset
)


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


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

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

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


# report status
return HestiaKERNEL-To-String-From-Unicode $___content
}
66 changes: 66 additions & 0 deletions init/services/HestiaKERNEL/Trim_Right_String.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/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/Error_Codes.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Right_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/To_Unicode_From_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/To_String_From_Unicode.sh"




HestiaKERNEL_Trim_Right_String() {
#___content="$1"
#___charset="$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_Right_Unicode "$___content" "$___chars")"
if [ "$___content" = "" ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_BAD_EXEC
fi

___content="$(HestiaKERNEL_To_String_From_Unicode "$___content")"
if [ "$___content" = "" ]; then
printf -- "%s" "$1"
return $HestiaKERNEL_ERROR_BAD_EXEC
fi
printf -- "%s" "$___content"


# report status
return $HestiaKERNEL_ERROR_OK
}
63 changes: 63 additions & 0 deletions init/services/HestiaKERNEL/Trim_Right_Unicode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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\Is_Unicode.ps1"




function HestiaKERNEL-Trim-Right-Unicode {
param (
[uint32[]]$___content_unicode,
[uint32[]]$___charset_unicode
)


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


# execute
[System.Collections.Generic.List[uint32]]$___converted = @()
$___is_scanning = 0
:scan_unicode for ($i = $___content_unicode.Length - 1; $i -ge 0; $i--) {
# get current character
$___current = $___content_unicode[$i]


# it's already mismatched so prefix the remaining values
if ($___is_scanning -ne 0) {
$null = $converted.Insert(0, $___current)
continue scan_unicode
}


# scan character from given charset
foreach ($___char in $___charset_unicode) {
if ($___current -eq $___char) {
continue scan_unicode # exit early from O(m^2) timing ASAP
}
}


# It's an mismatched
$___is_scanning = 1
$null = $converted.Insert(0, $___current)
}


# report status
return [uint32[]]$___converted
}
79 changes: 79 additions & 0 deletions init/services/HestiaKERNEL/Trim_Right_Unicode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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/Error_Codes.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Is_Unicode.sh"




HestiaKERNEL_Trim_Right_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
## IMPORTANT NOTICE
## POSIX Shell's UNIX regex cannot recognize anything outside Latin-1
## script. Therefore, manual algorithmic handling is required.
___content_unicode="$1"
___charset_unicode="$2"
___converted=""
while [ ! "$___content_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 char from charset character
___charset_list="$___charset_unicode"
___mismatched=0
while [ ! "$___charset_list" = "" ]; do
___char="${___charset_list%%, *}"
___charset_list="${___charset_list#"$___char"}"
if [ "${___charset_list%"${___charset_list#?}"}" = "," ]; then
___charset_list="${___charset_list#, }"
fi

if [ "$___current" = "$___char" ]; then
___charset_list=""
___mismatched=1
break # exit early from O(m^2) timing ASAP
fi
done


# It's a mismatch - prepend the rest and bail out
if [ $___mismatched -eq 0 ]; then
___converted="${___content_unicode}, ${___current}"
break
fi
done


# report status
printf -- "%s" "${___converted%, }"
return $HestiaKERNEL_ERROR_OK
}
4 changes: 4 additions & 0 deletions init/services/HestiaKERNEL/Vanilla.sh.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ echo \" <<'RUN_AS_POWERSHELL' >/dev/null # " | Out-Null
. "${env:LIBS_HESTIA}\HestiaKERNEL\To_UTF16_From_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\To_UTF32_From_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Trim_Left_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Trim_Right_String.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Trim_Left_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Trim_Right_Unicode.ps1"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Unicode.ps1"
################################################################################
# Windows POWERSHELL Codes #
Expand Down Expand Up @@ -91,7 +93,9 @@ RUN_AS_POWERSHELL
. "${LIBS_HESTIA}/HestiaKERNEL/To_UTF16_From_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/To_UTF32_From_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Left_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Right_String.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Left_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Right_Unicode.sh"
. "${LIBS_HESTIA}/HestiaKERNEL/Unicode.sh"
################################################################################
# Unix Main Codes #
Expand Down
2 changes: 2 additions & 0 deletions init/start.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ ${env:LIBS_HESTIA} = "${env:LIBS_UPSCALER}\services"
. "${env:LIBS_UPSCALER}\services\i18n\report-success.ps1"

### TEST ZONE
. "${env:LIBS_HESTIA}\HestiaKERNEL\Trim_Right_String.ps1"
Write-Host "$(HestiaKERNEL-Trim-Right-String "e你feeeff你你aerg aegE你F" "E你F")"
. "${env:LIBS_HESTIA}\HestiaKERNEL\Trim_Left_String.ps1"
Write-Host "$(HestiaKERNEL-Trim-Left-String "e你feeeff你你aerg aegE你F" "e你a")"

Expand Down
2 changes: 2 additions & 0 deletions init/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ LIBS_HESTIA="${LIBS_UPSCALER}/services"
. "${LIBS_UPSCALER}/services/i18n/report-success.sh"

### TEST ZONE
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Right_String.sh"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Right_String "e你feeeff你你aerg aegE你F" "E你F")"
. "${LIBS_HESTIA}/HestiaKERNEL/Trim_Left_String.sh"
1>&2 printf -- "%s\n" "$(HestiaKERNEL_Trim_Left_String "e你feeeff你你aerg aegE你F" "e你a")"

Expand Down

0 comments on commit 0250e86

Please sign in to comment.