forked from hollowaykeanho/Upscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init: Trim_Right_{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 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
1 parent
00a374e
commit 0250e86
Showing
9 changed files
with
282 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters