Cześć.
Można jakoś przetłumaczyć skrypt z języka KOTLIN na PHP ?
Z "ketchapem" wcześniej nie miałem nic wspólnego.. nie wiem za bardzo jak za to się zabrać
Jak próbowałem to wychodzą mi całkiem inne wyniki niż w "ketchapie"
skrypt który tłumaczę z githuba
import java.io.ByteArrayOutputStream
class PoiOutputStream : ByteArrayOutputStream() {
var position: Int
get() = count
set(value) {
count = position
}
fun writeIndex(index: Int, value: Int): Int {
val oldSize = size()
count = index
writeSwapInt(value)
val newSize = size()
count = oldSize
return newSize
}
fun writeSpecialStringWthLength(text: String) {
val bytes = text.toByteArray(charset("utf-8"))
if (bytes.size > text.length) {
write(15)
} else {
write(0)
}
writeSwapInt(bytes.size or Integer.MIN_VALUE)
write(bytes)
}
fun writeCodePointCount(str: String) {
var count = 0
val codePointCount = str.codePointCount(0, str.length)
for (position in 0 until codePointCount) {
count = combine(count, str.codePointAt(position))
}
writeSwapInt(count)
}
fun writeStringWithLength(text: String) {
val bytes = text.toByteArray(charset("utf-8"))
writeSwapInt(bytes.size or Integer.MIN_VALUE)
write(bytes)
}
fun writeUnicodeString(text: String) {
for (i in 0 until text.length) {
writeSwapShort(text.codePointAt(i))
}
}
fun writeString(t: String) {
write(t.toByteArray(Charsets.UTF_8))
}
fun writeSwapShort(value: Int) {
write(value)
write(value.ushr(8))
}
fun writeSwapInt(value: Int) {
write(value)
write(value.ushr(8))
write(value.ushr(16))
write(value.ushr(24)
)
}
fun writeInt(value: Int) {
write(value.ushr(24))
write(value.ushr(16))
write(value.ushr(8))
write(value)
}
private fun combine(val1: Int, val2: Int): Int {
val val3 = val1 shl 8
return (val3 and 0xFFFFFFF) + val2 xor (-0x10000000 and val3).ushr(24)
}
fun writeBoundingBox(boundingBox: BoundingBox) {
writeSwapInt(boundingBox.minLonInt - 1)
writeSwapInt(boundingBox.maxLatInt + 1)
writeSwapInt(boundingBox.maxLonInt + 1)
writeSwapInt(boundingBox.minLatInt - 1)
}
}
KOD PHP
<?php
class POISygic {
static function combine($val1, $val2) {
// shl?
#val val3 = val1 shl 8
$val3 = $int1 << 8;
#return (val3 and 0xFFFFFFF) + val2 xor (-0x10000000 and val3).ushr(24)
return ($val3 and 0xFFFFFFF) + $val2 ^ (-0x10000000 and $val2);
}
static function writeCodePointCount($string) {
$count = 0;
$codePointCount = strlen($string);
for($position = 0; $position < $codePointCount; $position++) {
$count = self::combine($count, self::codePoinAt($string, $position));
}
self::writeSwapInt($count); //
}
static function writeSwapInt($int) {
//tutaj nie wiem jaki jest odpowiednik funkcji `ushr` w PHP
#write(value) <- nie udało mi się również ustalić która fukcja obsługuje to.
#write(value.ushr(8))
#write(value.ushr(16))
#write(value.ushr(24))
//self::write(...);
}
static function writeSpecialStringWthLength($text) {
#val bytes = text.toByteArray(charset("utf-8"))
$byte = unpack('C*', utf8_encode($text));
if(mb_strlen($byte) > strlen($text)) {
#write(15);
self::write(15);
} else {
#write(0);
self::write(0);
}
}
static function writeUnicodeString($text) {
for($i = 0; $i < strlen(text); $i++) {
self::writeSwapShort(self::codePointAt($text, $i));
}
}
static function codePointAt($string, $index) {
// Tu jest OK.
if(($index < 0) OR ($index > strlen($string))) {
die("Index jest błędny!")
}
return substr($string, $index, 1);
}
static function writeString($string) {
self::write(unpack('C*', utf8_encode($string)));
}
static function writeStringWithLength($string) {
#val bytes = text.toByteArray(charset("utf-8"))
$bytes = unpack('C*', utf8_encode($string));
#writeSwapInt(bytes.size or Integer.MIN_VALUE) / Integer.MIN_VALUE <- jest w pliku 'PoiBlock.kt'
self::writeSwapInt(strlen($byte) OR self::MIN_VALUE);
self::write($bytes);
}
}
?>