Laravel AT Protocol Client (alpha & unstable)
at main 58 lines 1.6 kB view raw
1<?php 2 3namespace SocialDept\AtpClient\RichText; 4 5class ByteCounter 6{ 7 /** 8 * Get the byte length of a UTF-8 string 9 */ 10 public static function length(string $text): int 11 { 12 return strlen($text); 13 } 14 15 /** 16 * Get byte position of character at given index 17 */ 18 public static function bytePosition(string $text, int $charIndex): int 19 { 20 $chars = mb_str_split($text, 1, 'UTF-8'); 21 $bytePos = 0; 22 23 for ($i = 0; $i < $charIndex && $i < count($chars); $i++) { 24 $bytePos += strlen($chars[$i]); 25 } 26 27 return $bytePos; 28 } 29 30 /** 31 * Get substring by byte positions 32 */ 33 public static function substring(string $text, int $byteStart, int $byteEnd): string 34 { 35 return substr($text, $byteStart, $byteEnd - $byteStart); 36 } 37 38 /** 39 * Validate byte positions don't split multi-byte characters 40 */ 41 public static function validateBytePositions(string $text, int $byteStart, int $byteEnd): bool 42 { 43 // Check if positions are within bounds 44 if ($byteStart < 0 || $byteEnd > strlen($text) || $byteStart > $byteEnd) { 45 return false; 46 } 47 48 // Ensure we're not splitting a multi-byte character 49 $before = substr($text, 0, $byteStart); 50 $middle = substr($text, $byteStart, $byteEnd - $byteStart); 51 $after = substr($text, $byteEnd); 52 53 // Check if reconstructed string is valid UTF-8 54 return mb_check_encoding($before, 'UTF-8') 55 && mb_check_encoding($middle, 'UTF-8') 56 && mb_check_encoding($after, 'UTF-8'); 57 } 58}