Compare commits
56 Commits
1f30e07d39
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
8eb4752fe6
|
|||
|
e8baac9ba3
|
|||
|
d3a67c622c
|
|||
|
241ffd5e2c
|
|||
|
c80040466b
|
|||
|
20c2ba4c24
|
|||
|
47666a1e1a
|
|||
|
0129039df9
|
|||
|
3801c5759f
|
|||
|
03ec68cc40
|
|||
|
421beeed1e
|
|||
|
19b2fb36fd
|
|||
|
f18adc08b9
|
|||
|
85f10be1d2
|
|||
|
345be1cc6f
|
|||
|
d7d4e0c635
|
|||
|
3e769fe4d0
|
|||
|
17ff49ce0c
|
|||
|
d7e65ccc32
|
|||
|
dddf2bab93
|
|||
|
d55ec62be9
|
|||
|
9c7a9e4926
|
|||
|
ff0e17b5ad
|
|||
|
d06e523914
|
|||
|
afe35355a8
|
|||
|
947ef907a5
|
|||
|
23b565ca0a
|
|||
|
cc9961084c
|
|||
|
9391009403
|
|||
|
1195ad5acc
|
|||
|
9b7335ead2
|
|||
|
f5d559e8fe
|
|||
|
3ab2bea4f6
|
|||
|
a9f77d6211
|
|||
|
ecbeab8531
|
|||
|
b8639c7cf0
|
|||
|
300ad7511a
|
|||
|
22cfdd90ab
|
|||
|
01327ef4b2
|
|||
|
be9a8a4369
|
|||
|
96d2c5785f
|
|||
|
1e04a3aff4
|
|||
|
401d169cc0
|
|||
|
fa2c6b6621
|
|||
|
4fbadfba1e
|
|||
|
eb2cdbea73
|
|||
|
941a486217
|
|||
|
3a0a947fb0
|
|||
|
0daa5d68a4
|
|||
|
bd9531b60b
|
|||
|
f647abc0a4
|
|||
|
694c44d7d5
|
|||
|
e4c3a0f45f
|
|||
|
f825b6a7ab
|
|||
|
df3312f5c0
|
|||
|
2e2abc4f73
|
@@ -1,36 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
env:
|
|
||||||
GIT__TOKEN: ${{ secrets.TOKEN }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish:
|
|
||||||
runs-on: linux
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: actions/cache/restore@v4
|
|
||||||
with:
|
|
||||||
key: gradle
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches/
|
|
||||||
~/.gradle/wrapper/
|
|
||||||
~/.konan/
|
|
||||||
|
|
||||||
- uses: actions/setup-java@v4
|
|
||||||
with:
|
|
||||||
java-version: '24'
|
|
||||||
distribution: 'temurin'
|
|
||||||
|
|
||||||
- run: ./gradlew publish
|
|
||||||
|
|
||||||
- uses: actions/cache/save@v4
|
|
||||||
with:
|
|
||||||
key: gradle
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches/
|
|
||||||
~/.gradle/wrapper/
|
|
||||||
~/.konan/
|
|
||||||
47
.gitea/workflows/publish.yaml
Normal file
47
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: linux
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Java
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: '24'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Cache Gradle
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: gradle
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches/
|
||||||
|
~/.gradle/wrapper/
|
||||||
|
|
||||||
|
- name: Cache Konan
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: konan-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: konan
|
||||||
|
path: |
|
||||||
|
~/.konan/
|
||||||
|
|
||||||
|
- name: Cache Node
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: node-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: node
|
||||||
|
path: |
|
||||||
|
~/.gradle/nodejs
|
||||||
|
~/.gradle/yarn
|
||||||
|
|
||||||
|
- name: Publish
|
||||||
|
run: ./gradlew publish
|
||||||
|
env:
|
||||||
|
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
|
||||||
28
.gitea/workflows/test.yaml
Normal file
28
.gitea/workflows/test.yaml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: linux
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Java
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: '24'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Cache Gradle
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: gradle
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches/
|
||||||
|
~/.gradle/wrapper/
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: ./gradlew jvmTest
|
||||||
17
README.md
Normal file
17
README.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# bytes-kt
|
||||||
|
|
||||||
|
`bytes-kt` is a Kotlin Multiplatform library that provides helper functions for working with bytes.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add the following to your `build.gradle.kts`.
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
}
|
||||||
|
|
||||||
|
commonMain.dependencies {
|
||||||
|
implementation("com.infendro:bytes:1.4.2")
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,48 +1,40 @@
|
|||||||
group = "com.infendro"
|
@file:OptIn(ExperimentalWasmDsl::class)
|
||||||
version = "1.1.1"
|
|
||||||
|
|
||||||
repositories {
|
import com.infendro.plugin.infendro
|
||||||
mavenCentral()
|
import com.infendro.plugin.token
|
||||||
}
|
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||||
|
|
||||||
|
group = "com.infendro"
|
||||||
|
version = "1.4.2"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
alias(libs.plugins.infendro)
|
||||||
alias(libs.plugins.multiplatform)
|
alias(libs.plugins.multiplatform)
|
||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm()
|
jvm()
|
||||||
js {
|
|
||||||
nodejs()
|
|
||||||
}
|
|
||||||
linuxX64()
|
linuxX64()
|
||||||
}
|
linuxArm64()
|
||||||
|
mingwX64()
|
||||||
|
macosX64()
|
||||||
|
macosArm64()
|
||||||
|
js { nodejs() }
|
||||||
|
wasmJs { nodejs() }
|
||||||
|
wasmWasi { nodejs() }
|
||||||
|
|
||||||
publishing {
|
|
||||||
repositories {
|
repositories {
|
||||||
maven {
|
mavenCentral()
|
||||||
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
|
|
||||||
authentication.create("header", HttpHeaderAuthentication::class)
|
|
||||||
credentials(HttpHeaderCredentials::class) {
|
|
||||||
val token = secret("git.token") ?: throw GradleException()
|
|
||||||
|
|
||||||
name = "Authorization"
|
|
||||||
value = "token $token"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonTest.dependencies {
|
||||||
|
implementation(libs.test)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun secret(
|
publishing.repositories {
|
||||||
key: String,
|
infendro(token)
|
||||||
): String? {
|
|
||||||
val property = key
|
|
||||||
val environment = key
|
|
||||||
.uppercase()
|
|
||||||
.replace(".", "__")
|
|
||||||
|
|
||||||
val prop = properties[property] as String?
|
|
||||||
val env = System.getenv(environment)
|
|
||||||
|
|
||||||
return prop ?: env
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.1.20"
|
infendro = "1.1.0"
|
||||||
|
kotlin = "2.3.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||||
|
|
||||||
|
[libraries]
|
||||||
|
test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
|
||||||
|
|||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
5
gradle/wrapper/gradle-wrapper.properties
vendored
5
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,7 @@
|
|||||||
#Sun May 18 00:11:37 CEST 2025
|
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
47
gradlew
vendored
47
gradlew
vendored
@@ -15,6 +15,8 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
#
|
#
|
||||||
@@ -55,7 +57,7 @@
|
|||||||
# Darwin, MinGW, and NonStop.
|
# Darwin, MinGW, and NonStop.
|
||||||
#
|
#
|
||||||
# (3) This script is generated from the Groovy template
|
# (3) This script is generated from the Groovy template
|
||||||
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
# within the Gradle project.
|
# within the Gradle project.
|
||||||
#
|
#
|
||||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
@@ -80,13 +82,11 @@ do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
# This is normally unused
|
||||||
|
# shellcheck disable=SC2034
|
||||||
APP_NAME="Gradle"
|
|
||||||
APP_BASE_NAME=${0##*/}
|
APP_BASE_NAME=${0##*/}
|
||||||
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
MAX_FD=maximum
|
MAX_FD=maximum
|
||||||
@@ -114,7 +114,7 @@ case "$( uname )" in #(
|
|||||||
NONSTOP* ) nonstop=true ;;
|
NONSTOP* ) nonstop=true ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
CLASSPATH="\\\"\\\""
|
||||||
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
# Determine the Java command to use to start the JVM.
|
||||||
@@ -133,22 +133,29 @@ location of your Java installation."
|
|||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
JAVACMD=java
|
JAVACMD=java
|
||||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
if ! command -v java >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
Please set the JAVA_HOME variable in your environment to match the
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
location of your Java installation."
|
location of your Java installation."
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# Increase the maximum file descriptors if we can.
|
# Increase the maximum file descriptors if we can.
|
||||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||||
case $MAX_FD in #(
|
case $MAX_FD in #(
|
||||||
max*)
|
max*)
|
||||||
|
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
MAX_FD=$( ulimit -H -n ) ||
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
warn "Could not query maximum file descriptor limit"
|
warn "Could not query maximum file descriptor limit"
|
||||||
esac
|
esac
|
||||||
case $MAX_FD in #(
|
case $MAX_FD in #(
|
||||||
'' | soft) :;; #(
|
'' | soft) :;; #(
|
||||||
*)
|
*)
|
||||||
|
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
ulimit -n "$MAX_FD" ||
|
ulimit -n "$MAX_FD" ||
|
||||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
esac
|
esac
|
||||||
@@ -193,18 +200,28 @@ if "$cygwin" || "$msys" ; then
|
|||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Collect all arguments for the java command;
|
|
||||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
# shell script including quotes and variable substitutions, so put them in
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
# double quotes to make sure that they get re-expanded; and
|
|
||||||
# * put everything else in single quotes, so that it's not re-expanded.
|
# Collect all arguments for the java command:
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
|
# and any embedded shellness will be escaped.
|
||||||
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
|
# treated as '${Hostname}' itself on the command line.
|
||||||
|
|
||||||
set -- \
|
set -- \
|
||||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
-classpath "$CLASSPATH" \
|
-classpath "$CLASSPATH" \
|
||||||
org.gradle.wrapper.GradleWrapperMain \
|
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||||
"$@"
|
"$@"
|
||||||
|
|
||||||
|
# Stop when "xargs" is not available.
|
||||||
|
if ! command -v xargs >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "xargs is not available"
|
||||||
|
fi
|
||||||
|
|
||||||
# Use "xargs" to parse quoted args.
|
# Use "xargs" to parse quoted args.
|
||||||
#
|
#
|
||||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||||
|
|||||||
37
gradlew.bat
vendored
37
gradlew.bat
vendored
@@ -13,6 +13,8 @@
|
|||||||
@rem See the License for the specific language governing permissions and
|
@rem See the License for the specific language governing permissions and
|
||||||
@rem limitations under the License.
|
@rem limitations under the License.
|
||||||
@rem
|
@rem
|
||||||
|
@rem SPDX-License-Identifier: Apache-2.0
|
||||||
|
@rem
|
||||||
|
|
||||||
@if "%DEBUG%"=="" @echo off
|
@if "%DEBUG%"=="" @echo off
|
||||||
@rem ##########################################################################
|
@rem ##########################################################################
|
||||||
@@ -26,6 +28,7 @@ if "%OS%"=="Windows_NT" setlocal
|
|||||||
|
|
||||||
set DIRNAME=%~dp0
|
set DIRNAME=%~dp0
|
||||||
if "%DIRNAME%"=="" set DIRNAME=.
|
if "%DIRNAME%"=="" set DIRNAME=.
|
||||||
|
@rem This is normally unused
|
||||||
set APP_BASE_NAME=%~n0
|
set APP_BASE_NAME=%~n0
|
||||||
set APP_HOME=%DIRNAME%
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
@@ -40,13 +43,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome
|
|||||||
|
|
||||||
set JAVA_EXE=java.exe
|
set JAVA_EXE=java.exe
|
||||||
%JAVA_EXE% -version >NUL 2>&1
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
if "%ERRORLEVEL%" == "0" goto execute
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
@@ -56,32 +59,34 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|||||||
|
|
||||||
if exist "%JAVA_EXE%" goto execute
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
:execute
|
:execute
|
||||||
@rem Setup the command line
|
@rem Setup the command line
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
set CLASSPATH=
|
||||||
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
@rem Execute Gradle
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
|
||||||
|
|
||||||
:end
|
:end
|
||||||
@rem End local scope for the variables with windows NT shell
|
@rem End local scope for the variables with windows NT shell
|
||||||
if "%ERRORLEVEL%"=="0" goto mainEnd
|
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||||
|
|
||||||
:fail
|
:fail
|
||||||
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
rem the _cmd.exe /c_ return code!
|
rem the _cmd.exe /c_ return code!
|
||||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
set EXIT_CODE=%ERRORLEVEL%
|
||||||
exit /b 1
|
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||||
|
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||||
|
exit /b %EXIT_CODE%
|
||||||
|
|
||||||
:mainEnd
|
:mainEnd
|
||||||
if "%OS%"=="Windows_NT" endlocal
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
rootProject.name = "bytearray"
|
rootProject.name = "bytes"
|
||||||
|
|
||||||
|
pluginManagement.repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
enum class ByteOrder {
|
|
||||||
BIG_ENDIAN {
|
|
||||||
override fun normalize(
|
|
||||||
bytes: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
return bytes
|
|
||||||
}
|
|
||||||
},
|
|
||||||
LITTLE_ENDIAN {
|
|
||||||
override fun normalize(
|
|
||||||
bytes: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
return bytes.reversedArray()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
internal abstract fun normalize(
|
|
||||||
bytes: ByteArray,
|
|
||||||
): ByteArray
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
fun MutableCollection<Byte>.addAll(
|
|
||||||
bytes: ByteArray,
|
|
||||||
): Boolean {
|
|
||||||
return addAll(bytes.toList())
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun Int.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = ByteArray(4) { i ->
|
|
||||||
val offset = (3 - i) * 8
|
|
||||||
(this ushr offset).toByte()
|
|
||||||
}
|
|
||||||
return order.normalize(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toInt(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): Int {
|
|
||||||
if (size != 4) throw Exception()
|
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
|
||||||
return bytes.fold(0) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toInt() and 0xFF)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toInt(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): Int {
|
|
||||||
return toByteArray()
|
|
||||||
.toInt(order)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toIntArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): IntArray {
|
|
||||||
if (size % 4 != 0) throw Exception()
|
|
||||||
|
|
||||||
return toList()
|
|
||||||
.chunked(4)
|
|
||||||
.map { it.toInt(order) }
|
|
||||||
.toIntArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toIntArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): IntArray {
|
|
||||||
return toByteArray()
|
|
||||||
.toIntArray(order)
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun Long.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = ByteArray(8) { i ->
|
|
||||||
val offset = (7 - i) * 8
|
|
||||||
(this ushr offset).toByte()
|
|
||||||
}
|
|
||||||
return order.normalize(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toLong(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): Long {
|
|
||||||
if (size != 8) throw Exception()
|
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
|
||||||
return bytes.fold(0L) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toLong() and 0xFFL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toLong(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): Long {
|
|
||||||
return toByteArray()
|
|
||||||
.toLong(order)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toLongArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): LongArray {
|
|
||||||
if (size % 8 != 0) throw Exception()
|
|
||||||
|
|
||||||
return toList()
|
|
||||||
.chunked(8)
|
|
||||||
.map { it.toLong(order) }
|
|
||||||
.toLongArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toLongArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): LongArray {
|
|
||||||
return toByteArray()
|
|
||||||
.toLongArray(order)
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun UInt.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = ByteArray(4) { i ->
|
|
||||||
val offset = (3 - i) * 8
|
|
||||||
(this shr offset).toByte()
|
|
||||||
}
|
|
||||||
return order.normalize(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toUInt(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UInt {
|
|
||||||
if (size != 4) throw Exception()
|
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
|
||||||
return bytes.fold(0U) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toUInt() and 0xFFU)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toUInt(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UInt {
|
|
||||||
return toByteArray()
|
|
||||||
.toUInt(order)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toUIntArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UIntArray {
|
|
||||||
if (size % 4 != 0) throw Exception()
|
|
||||||
|
|
||||||
return toList()
|
|
||||||
.chunked(4)
|
|
||||||
.map { it.toUInt(order) }
|
|
||||||
.toUIntArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toUIntArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): UIntArray {
|
|
||||||
return toByteArray()
|
|
||||||
.toUIntArray(order)
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
import com.infendro.bytearray.ByteOrder.BIG_ENDIAN
|
|
||||||
|
|
||||||
fun ULong.toByteArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ByteArray {
|
|
||||||
val bytes = ByteArray(8) { i ->
|
|
||||||
val offset = (7 - i) * 8
|
|
||||||
(this shr offset).toByte()
|
|
||||||
}
|
|
||||||
return order.normalize(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toULong(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ULong {
|
|
||||||
if (size != 8) throw Exception()
|
|
||||||
|
|
||||||
val bytes = order.normalize(this)
|
|
||||||
return bytes.fold(0UL) { acc, byte ->
|
|
||||||
(acc shl 8) or (byte.toULong() and 0xFFUL)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toULong(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ULong {
|
|
||||||
return toByteArray()
|
|
||||||
.toULong(order)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.toULongArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ULongArray {
|
|
||||||
if (size % 8 != 0) throw Exception()
|
|
||||||
|
|
||||||
return toList()
|
|
||||||
.chunked(8)
|
|
||||||
.map { it.toULong(order) }
|
|
||||||
.toULongArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Collection<Byte>.toULongArray(
|
|
||||||
order: ByteOrder = BIG_ENDIAN,
|
|
||||||
): ULongArray {
|
|
||||||
return toByteArray()
|
|
||||||
.toULongArray(order)
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package com.infendro.bytearray
|
|
||||||
|
|
||||||
import kotlin.experimental.and
|
|
||||||
import kotlin.experimental.or
|
|
||||||
import kotlin.experimental.xor
|
|
||||||
|
|
||||||
infix fun ByteArray.and(
|
|
||||||
that: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
if (this.size != that.size) throw Exception()
|
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
|
||||||
this[i] and that[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun ByteArray.or(
|
|
||||||
that: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
if (this.size != that.size) throw Exception()
|
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
|
||||||
this[i] or that[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun ByteArray.xor(
|
|
||||||
that: ByteArray,
|
|
||||||
): ByteArray {
|
|
||||||
if (this.size != that.size) throw Exception()
|
|
||||||
|
|
||||||
return ByteArray(size) { i ->
|
|
||||||
this[i] xor that[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
5
src/commonMain/kotlin/com/infendro/bytes/ByteOrder.kt
Normal file
5
src/commonMain/kotlin/com/infendro/bytes/ByteOrder.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.bytes
|
||||||
|
|
||||||
|
enum class ByteOrder {
|
||||||
|
BIG_ENDIAN, LITTLE_ENDIAN;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.infendro.bytes.byte
|
||||||
|
|
||||||
|
import kotlin.experimental.inv
|
||||||
|
|
||||||
|
operator fun Byte.not(): Byte {
|
||||||
|
return inv()
|
||||||
|
}
|
||||||
145
src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt
Normal file
145
src/commonMain/kotlin/com/infendro/bytes/bytearray/Conversion.kt
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package com.infendro.bytes.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder
|
||||||
|
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
|
||||||
|
fun ByteArray.toInt(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Int {
|
||||||
|
require(startIndex in 0..(size - 4))
|
||||||
|
|
||||||
|
return when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
((this[startIndex].toInt() and 0xFF) shl 24) or
|
||||||
|
((this[startIndex + 1].toInt() and 0xFF) shl 16) or
|
||||||
|
((this[startIndex + 2].toInt() and 0xFF) shl 8) or
|
||||||
|
(this[startIndex + 3].toInt() and 0xFF)
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
(this[startIndex].toInt() and 0xFF) or
|
||||||
|
((this[startIndex + 1].toInt() and 0xFF) shl 8) or
|
||||||
|
((this[startIndex + 2].toInt() and 0xFF) shl 16) or
|
||||||
|
((this[startIndex + 3].toInt() and 0xFF) shl 24)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toLong(startIndex: Int = 0, order: ByteOrder = BIG_ENDIAN): Long {
|
||||||
|
require(startIndex in 0..(size - 8))
|
||||||
|
|
||||||
|
return when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
((this[startIndex].toLong() and 0xFFL) shl 56) or
|
||||||
|
((this[startIndex + 1].toLong() and 0xFFL) shl 48) or
|
||||||
|
((this[startIndex + 2].toLong() and 0xFFL) shl 40) or
|
||||||
|
((this[startIndex + 3].toLong() and 0xFFL) shl 32) or
|
||||||
|
((this[startIndex + 4].toLong() and 0xFFL) shl 24) or
|
||||||
|
((this[startIndex + 5].toLong() and 0xFFL) shl 16) or
|
||||||
|
((this[startIndex + 6].toLong() and 0xFFL) shl 8) or
|
||||||
|
(this[startIndex + 7].toLong() and 0xFFL)
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
(this[startIndex].toLong() and 0xFFL) or
|
||||||
|
((this[startIndex + 1].toLong() and 0xFFL) shl 8) or
|
||||||
|
((this[startIndex + 2].toLong() and 0xFFL) shl 16) or
|
||||||
|
((this[startIndex + 3].toLong() and 0xFFL) shl 24) or
|
||||||
|
((this[startIndex + 4].toLong() and 0xFFL) shl 32) or
|
||||||
|
((this[startIndex + 5].toLong() and 0xFFL) shl 40) or
|
||||||
|
((this[startIndex + 6].toLong() and 0xFFL) shl 48) or
|
||||||
|
((this[startIndex + 7].toLong() and 0xFFL) shl 56)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.copyInto(
|
||||||
|
destination: IntArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0 && length % 4 == 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - (length / 4)))
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
repeat(length / 4) { i ->
|
||||||
|
val offset = startIndex + (i * 4)
|
||||||
|
destination[destinationOffset + i] =
|
||||||
|
((this[offset].toInt() and 0xFF) shl 24) or
|
||||||
|
((this[offset + 1].toInt() and 0xFF) shl 16) or
|
||||||
|
((this[offset + 2].toInt() and 0xFF) shl 8) or
|
||||||
|
(this[offset + 3].toInt() and 0xFF)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
repeat(length / 4) { i ->
|
||||||
|
val offset = startIndex + (i * 4)
|
||||||
|
destination[destinationOffset + i] =
|
||||||
|
(this[offset].toInt() and 0xFF) or
|
||||||
|
((this[offset + 1].toInt() and 0xFF) shl 8) or
|
||||||
|
((this[offset + 2].toInt() and 0xFF) shl 16) or
|
||||||
|
((this[offset + 3].toInt() and 0xFF) shl 24)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toIntArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): IntArray {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
val result = IntArray(length / 4)
|
||||||
|
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.copyInto(
|
||||||
|
destination: LongArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0 && length % 8 == 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - (length / 8)))
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
repeat(length / 8) { i ->
|
||||||
|
val offset = startIndex + (i * 8)
|
||||||
|
destination[destinationOffset + i] =
|
||||||
|
((this[offset].toLong() and 0xFFL) shl 56) or
|
||||||
|
((this[offset + 1].toLong() and 0xFFL) shl 48) or
|
||||||
|
((this[offset + 2].toLong() and 0xFFL) shl 40) or
|
||||||
|
((this[offset + 3].toLong() and 0xFFL) shl 32) or
|
||||||
|
((this[offset + 4].toLong() and 0xFFL) shl 24) or
|
||||||
|
((this[offset + 5].toLong() and 0xFFL) shl 16) or
|
||||||
|
((this[offset + 6].toLong() and 0xFFL) shl 8) or
|
||||||
|
(this[offset + 7].toLong() and 0xFFL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
repeat(length / 8) { i ->
|
||||||
|
val offset = startIndex + (i * 8)
|
||||||
|
destination[destinationOffset + i] =
|
||||||
|
(this[offset].toLong() and 0xFFL) or
|
||||||
|
((this[offset + 1].toLong() and 0xFFL) shl 8) or
|
||||||
|
((this[offset + 2].toLong() and 0xFFL) shl 16) or
|
||||||
|
((this[offset + 3].toLong() and 0xFFL) shl 24) or
|
||||||
|
((this[offset + 4].toLong() and 0xFFL) shl 32) or
|
||||||
|
((this[offset + 5].toLong() and 0xFFL) shl 40) or
|
||||||
|
((this[offset + 6].toLong() and 0xFFL) shl 48) or
|
||||||
|
((this[offset + 7].toLong() and 0xFFL) shl 56)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toLongArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): LongArray {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
val result = LongArray(length / 8)
|
||||||
|
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
188
src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt
Normal file
188
src/commonMain/kotlin/com/infendro/bytes/bytearray/Operator.kt
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
package com.infendro.bytes.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytes.byte.not
|
||||||
|
import kotlin.experimental.and
|
||||||
|
import kotlin.experimental.or
|
||||||
|
import kotlin.experimental.xor
|
||||||
|
|
||||||
|
fun ByteArray.andInto(
|
||||||
|
that: ByteArray,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
thatStartIndex: Int = 0,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(thatStartIndex in 0..(that.size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.andInto(
|
||||||
|
that: Byte,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] and that
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||||
|
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||||
|
|
||||||
|
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||||
|
andInto(that, this, startIndex, startIndex, endIndex)
|
||||||
|
|
||||||
|
infix fun ByteArray.and(that: ByteArray): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
andInto(that, result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun ByteArray.and(that: Byte): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
andInto(that, result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.orInto(
|
||||||
|
that: ByteArray,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
thatStartIndex: Int = 0,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(thatStartIndex in 0..(that.size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.orInto(
|
||||||
|
that: Byte,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] or that
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.orWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||||
|
orInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||||
|
|
||||||
|
fun ByteArray.orWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||||
|
orInto(that, this, startIndex, startIndex, endIndex)
|
||||||
|
|
||||||
|
infix fun ByteArray.or(that: ByteArray): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
orInto(that, result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun ByteArray.or(that: Byte): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
orInto(that, result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.xorInto(
|
||||||
|
that: ByteArray,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
thatStartIndex: Int = 0,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(thatStartIndex in 0..(that.size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.xorInto(
|
||||||
|
that: Byte,
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] xor that
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.xorWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int = 0) =
|
||||||
|
xorInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||||
|
|
||||||
|
fun ByteArray.xorWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||||
|
xorInto(that, this, startIndex, startIndex, endIndex)
|
||||||
|
|
||||||
|
infix fun ByteArray.xor(that: ByteArray): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
xorInto(that, result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
infix fun ByteArray.xor(that: Byte): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
xorInto(that, result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = !this[startIndex + i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
|
||||||
|
invInto(this, startIndex, startIndex, endIndex)
|
||||||
|
|
||||||
|
operator fun ByteArray.not(): ByteArray {
|
||||||
|
val result = ByteArray(size)
|
||||||
|
invInto(result)
|
||||||
|
return result
|
||||||
|
}
|
||||||
30
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
30
src/commonMain/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.infendro.bytes.int
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder
|
||||||
|
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
|
||||||
|
fun Int.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||||
|
require(destinationOffset in 0..(destination.size - 4))
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
destination[destinationOffset] = (this shr 24).toByte()
|
||||||
|
destination[destinationOffset + 1] = (this shr 16).toByte()
|
||||||
|
destination[destinationOffset + 2] = (this shr 8).toByte()
|
||||||
|
destination[destinationOffset + 3] = this.toByte()
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
destination[destinationOffset] = this.toByte()
|
||||||
|
destination[destinationOffset + 1] = (this shr 8).toByte()
|
||||||
|
destination[destinationOffset + 2] = (this shr 16).toByte()
|
||||||
|
destination[destinationOffset + 3] = (this shr 24).toByte()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Int.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||||
|
val result = ByteArray(4)
|
||||||
|
copyInto(result, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
5
src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
5
src/commonMain/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.bytes.int
|
||||||
|
|
||||||
|
operator fun Int.not(): Int {
|
||||||
|
return inv()
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.infendro.bytes.intarray
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder
|
||||||
|
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
|
||||||
|
fun IntArray.copyInto(
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - (length * 4)))
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
repeat(length) { i ->
|
||||||
|
val offset = destinationOffset + (i * 4)
|
||||||
|
val value = this[startIndex + i]
|
||||||
|
destination[offset] = (value shr 24).toByte()
|
||||||
|
destination[offset + 1] = (value shr 16).toByte()
|
||||||
|
destination[offset + 2] = (value shr 8).toByte()
|
||||||
|
destination[offset + 3] = value.toByte()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
repeat(length) { i ->
|
||||||
|
val offset = destinationOffset + (i * 4)
|
||||||
|
val value = this[startIndex + i]
|
||||||
|
destination[offset] = value.toByte()
|
||||||
|
destination[offset + 1] = (value shr 8).toByte()
|
||||||
|
destination[offset + 2] = (value shr 16).toByte()
|
||||||
|
destination[offset + 3] = (value shr 24).toByte()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun IntArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
val result = ByteArray(length * 4)
|
||||||
|
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
38
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
38
src/commonMain/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.infendro.bytes.long
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder
|
||||||
|
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
|
||||||
|
fun Long.copyInto(destination: ByteArray, destinationOffset: Int = 0, order: ByteOrder = BIG_ENDIAN) {
|
||||||
|
require(destinationOffset in 0..(destination.size - 8))
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
destination[destinationOffset] = (this shr 56).toByte()
|
||||||
|
destination[destinationOffset + 1] = (this shr 48).toByte()
|
||||||
|
destination[destinationOffset + 2] = (this shr 40).toByte()
|
||||||
|
destination[destinationOffset + 3] = (this shr 32).toByte()
|
||||||
|
destination[destinationOffset + 4] = (this shr 24).toByte()
|
||||||
|
destination[destinationOffset + 5] = (this shr 16).toByte()
|
||||||
|
destination[destinationOffset + 6] = (this shr 8).toByte()
|
||||||
|
destination[destinationOffset + 7] = this.toByte()
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
destination[destinationOffset] = this.toByte()
|
||||||
|
destination[destinationOffset + 1] = (this shr 8).toByte()
|
||||||
|
destination[destinationOffset + 2] = (this shr 16).toByte()
|
||||||
|
destination[destinationOffset + 3] = (this shr 24).toByte()
|
||||||
|
destination[destinationOffset + 4] = (this shr 32).toByte()
|
||||||
|
destination[destinationOffset + 5] = (this shr 40).toByte()
|
||||||
|
destination[destinationOffset + 6] = (this shr 48).toByte()
|
||||||
|
destination[destinationOffset + 7] = (this shr 56).toByte()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Long.toByteArray(order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||||
|
val result = ByteArray(8)
|
||||||
|
copyInto(result, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.bytes.long
|
||||||
|
|
||||||
|
operator fun Long.not(): Long {
|
||||||
|
return inv()
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.infendro.bytes.longarray
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder
|
||||||
|
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
|
||||||
|
fun LongArray.copyInto(
|
||||||
|
destination: ByteArray,
|
||||||
|
destinationOffset: Int = 0,
|
||||||
|
startIndex: Int = 0,
|
||||||
|
endIndex: Int = size,
|
||||||
|
order: ByteOrder = BIG_ENDIAN,
|
||||||
|
) {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - (length * 8)))
|
||||||
|
|
||||||
|
when (order) {
|
||||||
|
BIG_ENDIAN -> {
|
||||||
|
repeat(length) { i ->
|
||||||
|
val offset = destinationOffset + (i * 8)
|
||||||
|
val value = this[startIndex + i]
|
||||||
|
destination[offset] = (value shr 56).toByte()
|
||||||
|
destination[offset + 1] = (value shr 48).toByte()
|
||||||
|
destination[offset + 2] = (value shr 40).toByte()
|
||||||
|
destination[offset + 3] = (value shr 32).toByte()
|
||||||
|
destination[offset + 4] = (value shr 24).toByte()
|
||||||
|
destination[offset + 5] = (value shr 16).toByte()
|
||||||
|
destination[offset + 6] = (value shr 8).toByte()
|
||||||
|
destination[offset + 7] = value.toByte()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LITTLE_ENDIAN -> {
|
||||||
|
repeat(length) { i ->
|
||||||
|
val offset = destinationOffset + (i * 8)
|
||||||
|
val value = this[startIndex + i]
|
||||||
|
destination[offset] = value.toByte()
|
||||||
|
destination[offset + 1] = (value shr 8).toByte()
|
||||||
|
destination[offset + 2] = (value shr 16).toByte()
|
||||||
|
destination[offset + 3] = (value shr 24).toByte()
|
||||||
|
destination[offset + 4] = (value shr 32).toByte()
|
||||||
|
destination[offset + 5] = (value shr 40).toByte()
|
||||||
|
destination[offset + 6] = (value shr 48).toByte()
|
||||||
|
destination[offset + 7] = (value shr 56).toByte()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun LongArray.toByteArray(startIndex: Int = 0, endIndex: Int = size, order: ByteOrder = BIG_ENDIAN): ByteArray {
|
||||||
|
val length = endIndex - startIndex
|
||||||
|
val result = ByteArray(length * 8)
|
||||||
|
copyInto(result, startIndex = startIndex, endIndex = endIndex, order = order)
|
||||||
|
return result
|
||||||
|
}
|
||||||
13
src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/byte/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.infendro.bytes.byte
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class ByteOperatorTest {
|
||||||
|
@Test
|
||||||
|
fun not() {
|
||||||
|
val expected = (-0x01).toByte()
|
||||||
|
val actual = !0x00.toByte()
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package com.infendro.bytes.bytearray
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class ByteArrayConversionTest {
|
||||||
|
@Test
|
||||||
|
fun toInt() {
|
||||||
|
val expected = 0x01234567
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67).toInt()
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(0x67, 0x45, 0x23, 0x01).toInt(order = LITTLE_ENDIAN)
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun toLong() {
|
||||||
|
val expected = 0x0123456789abcdefL
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11).toLong()
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01).toLong(order = LITTLE_ENDIAN)
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun copyIntoIntArray() {
|
||||||
|
val expected = intArrayOf(0, 0x01234567, 0x01234567)
|
||||||
|
val actual = IntArray(3).also { array ->
|
||||||
|
val bytes = byteArrayOf(
|
||||||
|
0x01, 0x23, 0x45, 0x67,
|
||||||
|
0x01, 0x23, 0x45, 0x67,
|
||||||
|
)
|
||||||
|
bytes.copyInto(array, 1)
|
||||||
|
}
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun toIntArray() {
|
||||||
|
val expected = intArrayOf(0x01234567, 0x67452301)
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(
|
||||||
|
0x01, 0x23, 0x45, 0x67,
|
||||||
|
0x67, 0x45, 0x23, 0x01,
|
||||||
|
).toIntArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(
|
||||||
|
0x67, 0x45, 0x23, 0x01,
|
||||||
|
0x01, 0x23, 0x45, 0x67,
|
||||||
|
).toIntArray(order = LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun copyIntoLongArray() {
|
||||||
|
val expected = longArrayOf(0, 0x0123456789abcdefL, 0x0123456789abcdefL)
|
||||||
|
val actual = LongArray(3).also { array ->
|
||||||
|
val bytes = byteArrayOf(
|
||||||
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
|
)
|
||||||
|
bytes.copyInto(array, 1)
|
||||||
|
}
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun toLongArray() {
|
||||||
|
val expected = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL)
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(
|
||||||
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
|
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||||
|
).toLongArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = byteArrayOf(
|
||||||
|
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||||
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
|
).toLongArray(order = LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package com.infendro.bytes.bytearray
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
|
||||||
|
class ByteArrayOperatorTest {
|
||||||
|
@Test
|
||||||
|
fun and() {
|
||||||
|
val expected = byteArrayOf(0x18, 0x00)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) and byteArrayOf(0x3A, 0x0F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun andByte() {
|
||||||
|
val expected = byteArrayOf(0x0C, 0x00)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) and 0x0F
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun or() {
|
||||||
|
val expected = byteArrayOf(0x7E, 0x7F)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) or byteArrayOf(0x3A, 0x0F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun orByte() {
|
||||||
|
val expected = byteArrayOf(0x5F, 0x7F)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) or 0x0F
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun xor() {
|
||||||
|
val expected = byteArrayOf(0x66, 0x7F)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) xor byteArrayOf(0x3A, 0x0F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun xorByte() {
|
||||||
|
val expected = byteArrayOf(0x53, 0x7F)
|
||||||
|
val actual = byteArrayOf(0x5C, 0x70) xor 0x0F
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun not() {
|
||||||
|
val expected = byteArrayOf(-0x01, -0x20)
|
||||||
|
val actual = !byteArrayOf(0x00, 0x1F)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
20
src/commonTest/kotlin/com/infendro/bytes/int/Conversion.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.infendro.bytes.int
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
|
||||||
|
class IntConversionTest {
|
||||||
|
@Test
|
||||||
|
fun toByteArray() {
|
||||||
|
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67)
|
||||||
|
run {
|
||||||
|
val actual = 0x01234567.toByteArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = 0x67452301.toByteArray(LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/int/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.infendro.bytes.int
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class IntOperatorTest {
|
||||||
|
@Test
|
||||||
|
fun not() {
|
||||||
|
val expected = -0x00000001
|
||||||
|
val actual = !0x00000000
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.infendro.bytes.intarray
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
|
||||||
|
class IntArrayConversionTest {
|
||||||
|
@Test
|
||||||
|
fun toByteArray() {
|
||||||
|
val expected = byteArrayOf(
|
||||||
|
0x01, 0x23, 0x45, 0x67,
|
||||||
|
0x67, 0x45, 0x23, 0x01,
|
||||||
|
)
|
||||||
|
run {
|
||||||
|
val actual = intArrayOf(0x01234567, 0x67452301).toByteArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = intArrayOf(0x67452301, 0x01234567).toByteArray(order = LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
20
src/commonTest/kotlin/com/infendro/bytes/long/Conversion.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.infendro.bytes.long
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
|
||||||
|
class LongConversionTest {
|
||||||
|
@Test
|
||||||
|
fun toByteArray() {
|
||||||
|
val expected = byteArrayOf(0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11)
|
||||||
|
run {
|
||||||
|
val actual = 0x0123456789abcdefL.toByteArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = (-0x1032547698badcffL).toByteArray(LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt
Normal file
13
src/commonTest/kotlin/com/infendro/bytes/long/Operator.kt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.infendro.bytes.long
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class LongOperatorTest {
|
||||||
|
@Test
|
||||||
|
fun not() {
|
||||||
|
val expected = -0x0000000000000001L
|
||||||
|
val actual = !0x0000000000000000L
|
||||||
|
assertEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.infendro.bytes.longarray
|
||||||
|
|
||||||
|
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertContentEquals
|
||||||
|
|
||||||
|
class LongArrayConversionTest {
|
||||||
|
@Test
|
||||||
|
fun toByteArray() {
|
||||||
|
val expected = byteArrayOf(
|
||||||
|
0x01, 0x23, 0x45, 0x67, -0x77, -0x55, -0x33, -0x11,
|
||||||
|
-0x11, -0x33, -0x55, -0x77, 0x67, 0x45, 0x23, 0x01,
|
||||||
|
)
|
||||||
|
run {
|
||||||
|
val actual = longArrayOf(0x0123456789abcdefL, -0x1032547698badcffL).toByteArray()
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
run {
|
||||||
|
val actual = longArrayOf(-0x1032547698badcffL, 0x0123456789abcdefL).toByteArray(order = LITTLE_ENDIAN)
|
||||||
|
assertContentEquals(expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user