/** * Test unicode safe string truncation. */ public function test_str_max_bytes() { // These are all 3 byte characters, so this is a 12-byte string. $str = '言語設定'; $this->assertEquals(12, strlen($str)); // Step back, shortening the string 1 byte at a time. Should remove in 1 char chunks. $conv = core_text::str_max_bytes($str, 12); $this->assertEquals(12, strlen($conv)); $this->assertSame('言語設定', $conv); $conv = core_text::str_max_bytes($str, 11); $this->assertEquals(9, strlen($conv)); $this->assertSame('言語設', $conv); $conv = core_text::str_max_bytes($str, 10); $this->assertEquals(9, strlen($conv)); $this->assertSame('言語設', $conv); $conv = core_text::str_max_bytes($str, 9); $this->assertEquals(9, strlen($conv)); $this->assertSame('言語設', $conv); $conv = core_text::str_max_bytes($str, 8); $this->assertEquals(6, strlen($conv)); $this->assertSame('言語', $conv); // Now try a mixed byte string. $str = '言語設a定'; $this->assertEquals(13, strlen($str)); $conv = core_text::str_max_bytes($str, 11); $this->assertEquals(10, strlen($conv)); $this->assertSame('言語設a', $conv); $conv = core_text::str_max_bytes($str, 10); $this->assertEquals(10, strlen($conv)); $this->assertSame('言語設a', $conv); $conv = core_text::str_max_bytes($str, 9); $this->assertEquals(9, strlen($conv)); $this->assertSame('言語設', $conv); $conv = core_text::str_max_bytes($str, 8); $this->assertEquals(6, strlen($conv)); $this->assertSame('言語', $conv); // Test 0 byte case. $conv = core_text::str_max_bytes($str, 0); $this->assertEquals(0, strlen($conv)); $this->assertSame('', $conv); }
/** * Formats the timestamp according to the search engine needs. * * @param int $timestamp * @return string */ public static function format_string_for_engine($string) { // 2^15 default. We could convert this to a setting as is possible to // change the max in solr. return \core_text::str_max_bytes($string, 32766); }