| ... | ... | @@ -259,7 +259,7 @@ test "allocUpperString" { |
| 259 | 259 | std.testing.expect(std.mem.eql(u8, "ABCDEFGHIJKLMNOPQRST0234+💩!", result)); |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | | /// Return `true` if `a` and `b` are equal, case insensitive. |
| 262 | /// Compares strings `a` and `b` case insensitively and returns whether they are equal. |
| 263 | 263 | pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool { |
| 264 | 264 | if (a.len != b.len) return false; |
| 265 | 265 | for (a) |a_c, i| { |
| ... | ... | @@ -274,43 +274,20 @@ test "eqlIgnoreCase" { |
| 274 | 274 | std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!")); |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | | /// Return `true` if `a` and `b` are equal, case sensitive. |
| 278 | | pub fn eqlMatchCase(a: []const u8, b: []const u8) bool { |
| 279 | | if (a.len != b.len) return false; |
| 280 | | for (a) |a_c, i| { |
| 281 | | if (a_c != b[i]) return false; |
| 282 | | } |
| 283 | | return true; |
| 284 | | } |
| 285 | | |
| 286 | | test "eqlMatchCase" { |
| 287 | | std.testing.expect(!eqlMatchCase("HEl💩Lo!", "hel💩lo!")); |
| 288 | | std.testing.expect(eqlMatchCase("Hello! ", "Hello! ")); |
| 289 | | std.testing.expect(eqlMatchCase("hElLo!", "hElLo!")); |
| 290 | | } |
| 291 | | |
| 292 | | fn indexOfPos(container: []const u8, start_index: usize, substr: []const u8, comptime case_sensitive: bool) ?usize { |
| 277 | /// Finds `substr` in `container`, ignoring case, starting at `start_index`. |
| 278 | /// TODO boyer-moore algorithm |
| 279 | pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize { |
| 293 | 280 | if (substr.len > container.len) return null; |
| 294 | 281 | |
| 295 | 282 | var i: usize = start_index; |
| 296 | 283 | const end = container.len - substr.len; |
| 297 | 284 | while (i <= end) : (i += 1) { |
| 298 | | if (case_sensitive) { |
| 299 | | if (eqlMatchCase(container[i .. i + substr.len], substr)) return i; |
| 300 | | } else { |
| 301 | | if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i; |
| 302 | | } |
| 285 | if (eqlIgnoreCase(container[i .. i + substr.len], substr)) return i; |
| 303 | 286 | } |
| 304 | 287 | return null; |
| 305 | 288 | } |
| 306 | 289 | |
| 307 | | /// Finds `substr` in `container`, case insensitive, starting at `start_index`. |
| 308 | | /// TODO boyer-moore algorithm |
| 309 | | pub fn indexOfIgnoreCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize { |
| 310 | | return indexOfPos(container, start_index, substr, false); |
| 311 | | } |
| 312 | | |
| 313 | | /// Finds `substr` in `container`, case insensitive, starting at index 0. |
| 290 | /// Finds `substr` in `container`, ignoring case, starting at index 0. |
| 314 | 291 | pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize { |
| 315 | 292 | return indexOfIgnoreCasePos(container, 0, substr); |
| 316 | 293 | } |
| ... | ... | @@ -323,22 +300,3 @@ test "indexOfIgnoreCase" { |
| 323 | 300 | |
| 324 | 301 | std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0); |
| 325 | 302 | } |
| 326 | | |
| 327 | | /// Finds `substr` in `container`, case sensitive, starting at `start_index`. |
| 328 | | pub fn indexOfMatchCasePos(container: []const u8, start_index: usize, substr: []const u8) ?usize { |
| 329 | | return indexOfPos(container, start_index, substr, true); |
| 330 | | } |
| 331 | | |
| 332 | | /// Finds `substr` in `container`, case sensitive, starting at index 0. |
| 333 | | pub fn indexOfMatchCase(container: []const u8, substr: []const u8) ?usize { |
| 334 | | return indexOfMatchCasePos(container, 0, substr); |
| 335 | | } |
| 336 | | |
| 337 | | test "indexOfMatchCase" { |
| 338 | | std.testing.expect(indexOfMatchCase("one Two Three Four", "Four").? == 14); |
| 339 | | std.testing.expect(indexOfMatchCase("one two three FouR", "fOur") == null); |
| 340 | | std.testing.expect(indexOfMatchCase("foO", "foO").? == 0); |
| 341 | | std.testing.expect(indexOfMatchCase("foo", "fool") == null); |
| 342 | | |
| 343 | | std.testing.expect(indexOfMatchCase("FOO foo", "FOO").? == 0); |
| 344 | | } |