Skip to content

Commit a8a1a93

Browse files
committed
wip
1 parent 68eb288 commit a8a1a93

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-33
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3151,7 +3151,8 @@ public function testTranslateLikeBinary() {
31513151

31523152
// Test escaping - "\\\%" is "\" and a wildcard
31533153
$result = $this->assertQuery( "SELECT * FROM _tmp_table WHERE name LIKE BINARY 'special\\\\\\%chars'" );
3154-
$this->assertCount( 0, $result );
3154+
$this->assertCount( 1, $result );
3155+
$this->assertEquals( 'special\\chars', $result[0]->name );
31553156

31563157
// Test LIKE without BINARY
31573158
$result = $this->assertQuery( "SELECT * FROM _tmp_table WHERE name LIKE 'FIRST'" );

wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -794,41 +794,17 @@ public function _helper_like_to_glob_pattern( $pattern ) {
794794
$pattern = str_replace( '*', '[*]', $pattern );
795795
$pattern = str_replace( '?', '[?]', $pattern );
796796

797-
$glob_pattern = '';
798-
for ( $i = 0; $i < strlen( $pattern ); $i += 1 ) {
799-
$byte1 = $pattern[ $i ];
800-
if ( '\\' === $byte1 ) {
801-
// Add the escape character.
802-
$glob_pattern .= $byte1;
803-
804-
// Special case: "\\%" and "\\_" are equivalent to "\%" and "\_".
805-
// In such case, we need to skip the extra backslash.
806-
$byte2 = $pattern[ $i + 1 ] ?? null;
807-
$byte3 = $pattern[ $i + 2 ] ?? null;
808-
if ( '\\' === $byte2 && ( '%' === $byte3 || '_' === $byte3 ) ) {
809-
$glob_pattern .= $byte3;
810-
$i += 2;
811-
continue;
812-
}
813-
814-
// We're in an escape sequence. Add the next character as it is.
815-
$glob_pattern .= $byte2;
816-
$i += 1;
817-
} elseif ( '%' === $byte1 ) {
818-
$glob_pattern .= '*';
819-
} elseif ( '_' === $byte1 ) {
820-
$glob_pattern .= '?';
821-
} else {
822-
$glob_pattern .= $byte1;
823-
}
824-
}
797+
$pattern = preg_replace('/(^|[^\\\\](?:\\\\{2}))*(\\\\[%_])/', '$1\\\\$2', $pattern);
825798

826799
// 1. Unescape C-style escape sequences.
827-
$glob_pattern = stripcslashes($glob_pattern);
800+
$pattern = stripcslashes($pattern);
828801

829-
// 2. Unescape LIKE escape sequences.
830-
$glob_pattern = preg_replace('/\\\\(.)/', '$1', $glob_pattern);
802+
$pattern = preg_replace('/(^|[^\\\\](?:\\\\{2})*)%/', '$1*', $pattern);
803+
$pattern = preg_replace('/(^|[^\\\\](?:\\\\{2})*)_/', '$1?', $pattern);
831804

832-
return $glob_pattern;
805+
// 2. Unescape LIKE escape sequences.
806+
$pattern = preg_replace('/\\\\(.)/', '$1', $pattern);
807+
808+
return $pattern;
833809
}
834810
}

0 commit comments

Comments
 (0)