fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
25cc01845
master
1#RUN: fish=%fish %fish %s 2# Tests for string builtin. Mostly taken from man page examples. 3 4string 5# CHECKERR: string: missing subcommand 6# CHECKERR: {{.*}}checks/string.fish (line {{\d+}}): 7# CHECKERR: string 8# CHECKERR: ^ 9# CHECKERR: (Type 'help string' for related documentation) 10 11string abc 12# CHECKERR: string abc: invalid subcommand 13# CHECKERR: {{.*}}checks/string.fish (line {{\d+}}): 14# CHECKERR: string abc 15# CHECKERR: ^ 16# CHECKERR: (Type 'help string' for related documentation) 17 18string --abc 19# CHECKERR: string --abc: invalid subcommand 20# CHECKERR: {{.*}}checks/string.fish (line {{\d+}}): 21# CHECKERR: string --abc 22# CHECKERR: ^ 23# CHECKERR: (Type 'help string' for related documentation) 24 25string match -r -v"c.*" dog can cat diz;and echo "exit 0" 26# CHECK: dog 27# CHECK: diz 28# CHECK: exit 0 29 30string match -q -r -v"c.*" dog can cat diz;and echo "exit 0" 31# CHECK: exit 0 32 33string match -v"c*" dog can cat diz;and echo "exit 0" 34# CHECK: dog 35# CHECK: diz 36# CHECK: exit 0 37 38string match -q -v"c*" dog can cat diz;and echo "exit 0" 39# CHECK: exit 0 40 41string match -v"d*" dog dan dat diz;or echo "exit 1" 42# CHECK: exit 1 43 44string match -q -v"d*" dog dan dat diz;or echo "exit 1" 45# CHECK: exit 1 46 47string match -r -v x y;and echo "exit 0" 48# CHECK: y 49# CHECK: exit 0 50 51string match -r -v x x;or echo "exit 1" 52# CHECK: exit 1 53 54string match -q -r -v x y;and echo "exit 0" 55# CHECK: exit 0 56 57string match -q -r -v x x;or echo "exit 1" 58# CHECK: exit 1 59 60string match -v -g foo foo 61# CHECKERR: string match: invalid option combination, --invert and --groups-only are mutually exclusive 62 63string match 64# CHECKERR: string match: missing argument 65 66string length"hello, world" 67# CHECK: 12 68 69string length -q"" ;and echo not zero length;or echo zero length 70# CHECK: zero length 71 72string pad foo 73#CHECK: foo 74string pad -C foo 75# CHECK: foo 76 77string pad -r -w7 --char - foo 78# CHECK: foo---- 79string pad -r -w7 --chars - --center foo 80# CHECK: --foo-- 81 82# might overflow when converting sign 83string sub --start-9223372036854775808 abc 84# CHECK: abc 85string sub --start0 abc 86# CHECKERR: string sub: Invalid start value '0' 87 88string pad --width7 -c'=' foo 89# CHECK: ====foo 90string pad --width7 -c'=' -C foo 91# CHECK: ==foo== 92string pad --width8 -c'=' -C foo 93# CHECK: ===foo== 94string pad --width8 -c'=' -Cr foo 95# CHECK: ==foo=== 96 97echo \| ( string pad --width10 --right foo) \| 98# CHECK: |foo | 99echo \| ( string pad --width10 --right --center foo) \| 100# CHECK: | foo | 101echo \| ( string pad --width10 --center foo) \| 102# CHECK: | foo | 103 104begin 105set -l fish_emoji_width2 106# Pad string with multi-width emoji. 107string pad -w4 -c . 🐟 108# CHECK: ..🐟 109string pad -w4 -c . -C 🐟 110# CHECK: .🐟. 111 112# Pad with multi-width character. 113string pad -w3 -c 🐟 . 114# CHECK: 🐟. 115# string pad would rather the result actually be centerd, than it actually contain 116# the padding character (so since it can't print half a 🐟, it instead prints a space which is half as wide) 117string collect\| ( string pad -w3 -c 🐟 -C .) \| 118# CHECK: | . | 119string collect\| ( string pad -w7 -c 🐟 -C .) \| 120# CHECK: |🐟 . 🐟| 121 122# Multi-width pad with remainder, complemented with a space. 123string pad -w4 -c 🐟 . .. 124# CHECK: 🐟 . 125# CHECK: 🐟.. 126string collect\| ( string pad -w7 -c 🐟 -C . ..) \| 127# CHECK: |🐟 . 🐟| 128# CHECK: |🐟 ..🐟| 129string collect\| ( string pad -w7 -c 🐟 -Cr . ..) \| 130# CHECK: |🐟 . 🐟| 131# CHECK: |🐟.. 🐟| 132end 133 134# Pad to the maximum length. 135string pad -c . long longer longest 136# CHECK: ...long 137# CHECK: .longer 138# CHECK: longest 139string pad -c . -C long longer longest 140# CHECK: ..long. 141# CHECK: .longer 142# CHECK: longest 143string pad -c . -Cr long longer longest 144# CHECK: .long.. 145# CHECK: longer. 146# CHECK: longest 147 148# This tests current behavior where the max width of an argument overrules 149# the width parameter. This could be changed if needed. 150string pad -c_ --width5 longer-than-width-param x 151# CHECK: longer-than-width-param 152# CHECK: ______________________x 153string pad -c_ --width5 --center longer-than-width-param x 154# CHECK: longer-than-width-param 155# CHECK: ___________x___________ 156string pad -c_ --width5 --center --right longer-than-width-param x 157# CHECK: longer-than-width-param 158# CHECK: ___________x___________ 159 160# Current behavior is that only a single padding character is supported. 161# We can support longer strings in future without breaking compatibility. 162string pad -c ab -w4 . 163# CHECKERR: string pad: Padding should be a character 'ab' 164 165# nonprintable characters does not make sense 166string pad -c\u07 . 167# CHECKERR: string pad: Invalid padding character of width zero {{'\a'}} 168 169string pad --width=-1 foo 170# CHECKERR: string pad: Invalid width value '-1' 171 172# Visible length. Let's start off simple, colors are ignored: 173string length --visible( set_color red) abc 174# CHECK: 3 175begin 176set -l fish_emoji_width2 177# This should print the emoji width 178string length --visible .\U2693 179# CHECK: 1 180# CHECK: 2 181set -l fish_emoji_width1 182string length --visible .\U2693 183# CHECK: 1 184# CHECK: 1 185end 186 187# Only the longest run between carriage returns is kept because the rest is overwritten. 188string length --visible( set_color --reset) abcdef\r fooba( set_color red) raaa 189# (foobaraaa) 190# CHECK: 9 191 192# Visible length is *always* split by line 193string length --visible a( set_color blue) b\n cde 194# CHECK: 2 195# CHECK: 3 196 197# Backslashes and visible length: 198# It can't move us before the start of the line. 199string length --visible\b 200# CHECK: 0 201 202# It can't move us before the start of the line. 203string length --visible\b f 204# CHECK: 1 205 206# But it does erase chars before. 207string length --visible\b f\b 208# CHECK: 0 209 210# Never move past 0. 211string length --visible\b f\b\b\b\b\b 212# CHECK: 0 213 214string sub --length2 abcde 215# CHECK: ab 216 217string sub -s2 -l2 abcde 218# CHECK: bc 219 220string sub --length=-1 abcde 221# CHECKERR: string sub: Invalid length value '-1' 222 223string sub --start=-2 abcde 224# CHECK: de 225 226string sub --end=3 abcde 227# CHECK: abc 228 229string sub --end=-4 abcde 230# CHECK: a 231 232string sub --end=0 abcde 233# CHECKERR: string sub: Invalid end value '0' 234 235string sub --start=2 --end=-2 abcde 236# CHECK: bc 237 238string sub -s-5 -e-2 abcdefgh 239# CHECK: def 240 241string sub -s-100 -e-2 abcde 242# CHECK: abc 243 244string sub -s-5 -e2 abcde 245# CHECK: ab 246 247string sub -s-50 -e-100 abcde 248# CHECK: 249 250string sub -s2 -e-5 abcde 251# CHECK: 252 253string sub -s2 -e-5 -l3 abcde 254# CHECKERR: string sub: invalid option combination, --end and --length are mutually exclusive 255 256string split . example.com 257# CHECK: example 258# CHECK: com 259 260string split -r -m1 / /usr/local/bin/fish 261# CHECK: /usr/local/bin 262# CHECK: fish 263 264string split"" abc 265# CHECK: a 266# CHECK: b 267# CHECK: c 268 269string split 270# CHECKERR: string split: missing argument 271 272string split --max1 --right12 AB12CD 273# CHECK: AB 274# CHECK: CD 275 276string split --max=-1 --right12 AB12CD 277# CHECKERR: string split: Invalid max value '-1' 278 279string split --fields=2"" abc 280# CHECK: b 281 282string split --fields=3,2"" abc 283# CHECK: c 284# CHECK: b 285 286string split --fields=2,9"" abc;or echo "exit 1" 287# CHECK: exit 1 288 289string split --fields=2-3-,9"" a 290# CHECKERR: string split: 2-3-,9: invalid integer 291 292string split --fields=1-99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"" abc 293# CHECKERR: string split: 1-99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999: invalid integer 294 295string split --fields=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999-1"" abc 296# CHECKERR: string split: 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999-1: invalid integer 297 298string split --fields=1--2"" b 299# CHECKERR: string split: 1--2: invalid integer 300 301string split --fields=0"" c 302# CHECKERR: string split: Invalid fields value '0' 303 304string split --fields=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"" abc 305# CHECKERR: string split: 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999: invalid integer 306 307string split --fields=1-0"" d 308# CHECKERR: string split: Invalid range value for field '1-0' 309 310string split --fields=0-1"" e 311# CHECKERR: string split: Invalid range value for field '0-1' 312 313string split --fields=-1"" f 314# CHECKERR: string split: -1: invalid integer 315 316string split --fields=1a"" g 317# CHECKERR: string split: 1a: invalid integer 318 319string split --fields=a"" h 320# CHECKERR: string split: a: invalid integer 321 322string split --fields=1-3,5,9-7"" 123456789 323# CHECK: 1 324# CHECK: 2 325# CHECK: 3 326# CHECK: 5 327# CHECK: 9 328# CHECK: 8 329# CHECK: 7 330 331string split -f1' ' 'a b' 'c d' 332# CHECK: a 333# CHECK: c 334 335string split --allow-empty --fields=2,9"" abc 336# CHECK: b 337 338string split --allow-empty"" abc 339# CHECKERR: string split: invalid option combination, --allow-empty is only valid with --fields 340 341seq 3 | string join ... 342# CHECK: 1...2...3 343 344string join 345# CHECKERR: string join: missing argument 346 347string trim" abc " 348# CHECK: abc 349 350string trim --right --chars=yz xyzzy zany 351# CHECK: x 352# CHECK: zan 353 354echo \x07 | string escape 355# CHECK: \cg 356 357string escape --style=script'a b#c"\'d' 358# CHECK: 'a b#c"\'d' 359 360string escape --no-quoted --style=script'a b#c"\'d' 361# CHECK: a\ b#c\"\'d 362 363string escape --no-quoted --style=script'a #b' 364# CHECK: a\ \#b 365 366string escape --style=url'a b#c"\'d' 367# CHECK: a%20b%23c%22%27d 368 369string escape --style=url\n a\n b%c~d\n 370# CHECK: %0Aa%0Ab%25c~d%0A 371 372string escape --style=var'a b#c"\'d' 373# CHECK: a_20_b_23_c_22_27_d 374 375string escape --style=var a\n ghi_ 376# CHECK: a_0A_ghi__ 377 378string escape --style=var abc 379# CHECK: abc 380 381string escape --style=var _a_b_c_ 382# CHECK: __a__b__c__ 383 384string escape --style=var -- - 385# CHECK: _2D_ 386 387# string escape with multibyte chars 388string escape --style=url aöb 389string escape --style=url 中 390string escape --style=url aöb| string unescape --style=url 391string escape --style=url 中| string unescape --style=url 392 393string escape --style=var aöb 394string escape --style=var 中 395string escape --style=var aöb| string unescape --style=var 396string escape --style=var 中| string unescape --style=var 397# CHECK: a%C3%B6b 398# CHECK: %E4%B8%AD 399# CHECK: aöb 400# CHECK: 中 401# CHECK: a_C3_B6_b 402# CHECK: _E4_B8_AD_ 403# CHECK: aöb 404# CHECK: 中 405 406# test regex escaping 407string escape --style=regex".ext" 408string escape --style=regex"bonjour, amigo" 409string escape --style=regex"^this is a literal string" 410string escape --style=regex"hello 411world" 412# CHECK: \.ext 413# CHECK: bonjour, amigo 414# CHECK: \^this is a literal string 415# CHECK: hello\nworld 416 417string escape --style=unknown-style 418# CHECKERR: string escape: Invalid escape style 'unknown-style' 419 420### Verify that we can correctly unescape the same strings 421# we tested escaping above. 422set x( string unescape( echo \x07 | string escape)) 423test $x = \x07 424and echo success 425# CHECK: success 426 427string unescape --style=script( string escape --style=script'a b#c"\'d' ) 428# CHECK: a b#c"'d 429 430string unescape --style=url( string escape --style=url'a b#c"\'d' ) 431# CHECK: a b#c"'d 432 433string unescape --style=url( string escape --style=url\n a\n b%c~d\n ) 434# CHECK: 435# CHECK: a 436# CHECK: b%c~d 437 438string unescape --style=var( string escape --style=var'a b#c"\'d' ) 439# CHECK: a b#c"'d 440 441string unescape --style=var( string escape --style=var a\n ghi_) 442# CHECK: a 443# CHECK: ghi_ 444 445string unescape --style=var( string escape --style=var'abc' ) 446# CHECK: abc 447 448string unescape --style=var( string escape --style=var'_a_b_c_' ) 449# CHECK: _a_b_c_ 450 451string unescape --style=var --( string escape --style=var -- -) 452# CHECK: - 453 454string unescape --style=unknown-style 455# CHECKERR: string unescape: Invalid style value 'unknown-style' 456 457### Verify that we can correctly match strings. 458string match"*" a 459# CHECK: a 460 461string match"a*b" axxb 462# CHECK: axxb 463 464string match -i"a**B" Axxb 465# CHECK: Axxb 466 467echo "ok?" | string match"*?" 468# CHECK: ok? 469 470string match -r"cat|dog|fish" "nice dog" 471# CHECK: dog 472 473string match -r" ( \d\d? ) :(\d\d):(\d\d)" 2:34:56 474# CHECK: 2:34:56 475# CHECK: 2 476# CHECK: 34 477# CHECK: 56 478 479string match -r"^(\w{2,4})\g1\$" papa mud murmur 480# CHECK: papa 481# CHECK: pa 482# CHECK: murmur 483# CHECK: mur 484 485string match -r -a -n at ratatat 486# CHECK: 2 2 487# CHECK: 4 2 488# CHECK: 6 2 489 490string match -r -i"0x[0-9a-f]{1,8}" "int magic = 0xBadC0de;" 491# CHECK: 0xBadC0de 492 493string match -r -i"0x[0-9a-f]{1,8}" "int magic = 0xBadC0de;" 494# CHECK: 0xBadC0de 495 496string replace is was"blue is my favorite" 497# CHECK: blue was my favorite 498 499string replace 3rd last 1st 2nd 3rd 500# CHECK: 1st 501# CHECK: 2nd 502# CHECK: last 503 504string replace -a" " _"spaces to underscores" 505# CHECK: spaces_to_underscores 506 507string replace -r -a"[^\d.]+" " " "0 one two 3.14 four 5x" 508# CHECK: 0 3.14 5 509 510string replace -r" ( \w+ ) \s+(\w+)" "\$2 \$1 \$\$" "left right" 511# CHECK: right left $ 512 513string replace -r"\s*newline\s*" "\n" "put a newline here" 514# CHECK: put a 515# CHECK: here 516 517string replace -r -a" ( \w ) " "\$1\$1" ab 518# CHECK: aabb 519 520echo a| string replace b c -q 521or echo No replace fails 522# CHECK: No replace fails 523 524echo a| string replace -r b c -q 525or echo No replace regex fails 526# CHECK: No replace regex fails 527 528string replace --filter x X abc axc x def jkx 529or echo Unexpected exit status at line( status --current-line-number) 530# CHECK: aXc 531# CHECK: X 532# CHECK: jkX 533 534string replace --filter y Y abc axc x def jkx 535and echo Unexpected exit status at line( status --current-line-number) 536 537string replace --regex -f"\d" X 1bc axc2 d3f jk4 xyz 538or echo Unexpected exit status at line( status --current-line-number) 539# CHECK: Xbc 540# CHECK: X 541# CHECK: dXf 542# CHECK: jkX 543 544string replace --regex -f Z X 1bc axc2 d3f jk4 xyz 545and echo Unexpected exit status at line( status --current-line-number) 546 547# From https://github.com/fish-shell/fish-shell/issues/5201 548# 'string match -r with empty capture groups' 549string match -r'^([ugoa]*)([=+-]?)([rwx]*)$' '=r' 550#CHECK: =r 551#CHECK: 552#CHECK: = 553#CHECK: r 554 555### Test some failure cases 556string match -r"[" "a[sd" ;and echo "unexpected exit 0" 557# CHECKERR: string match: Regular expression compile error: missing terminating ] for character class 558# CHECKERR: string match: [ 559# CHECKERR: string match: ^ 560 561# FIXME: This prints usage summary? 562#string invalidarg; and echo "unexpected exit 0" 563# DONTCHECKERR: string: Subcommand 'invalidarg' is not valid 564 565string length;or echo "missing argument returns 1" 566# CHECK: missing argument returns 1 567 568string match -r -v"[dcantg].*" dog can cat diz;or echo "no regexp invert match" 569# CHECK: no regexp invert match 570 571string match -v"*" dog can cat diz;or echo "no glob invert match" 572# CHECK: no glob invert match 573 574string match -rvn a bbb;or echo "exit 1" 575# CHECK: 1 3 576 577### Test repeat subcommand 578string repeat -n2 foo 579# CHECK: foofoo 580 581string repeat --count2 foo 582# CHECK: foofoo 583 584string repeat2 foo 585# CHECK: foofoo 586 587string repeat2 -n3 588# CHECK: 222 589 590echo foo| string repeat -n2 591# CHECK: foofoo 592 593echo foo| string repeat2 594# CHECK: foofoo 595 596string repeat 597# CHECKERR: string repeat: missing argument 598 599string repeat foo 600# CHECKERR: string repeat: Invalid count value 'foo' 601 602string repeat -n2 -q foo;and echo "exit 0" 603# CHECK: exit 0 604 605string repeat -n2 --quiet foo;and echo "exit 0" 606# CHECK: exit 0 607 608string repeat -n0 foo;or echo "exit 1" 609# CHECK: exit 1 610 611string repeat -n0;or echo "exit 1" 612# CHECK: exit 1 613 614string repeat -m0;or echo "exit 1" 615# CHECK: exit 1 616 617string repeat -n1 -N"there is " 618echo "no newline" 619# CHECK: there is no newline 620 621string repeat -n1 --no-newline"there is " 622echo "no newline" 623# CHECK: there is no newline 624 625string repeat -n10 -m4 foo 626# CHECK: foof 627 628string repeat -n10 --max5 foo 629# CHECK: foofo 630 631string repeat -n3 -m20 foo 632# CHECK: foofoofoo 633 634string repeat -m4 foo 635# CHECK: foof 636 637string repeat -n5 a b c 638# CHECK: aaaaa 639# CHECK: bbbbb 640# CHECK: ccccc 641 642string repeat -n5 --max4 123 456 789 643# CHECK: 1231 644# CHECK: 4564 645# CHECK: 7897 646 647string repeat -n5 --max4 123 '' 789 648# CHECK: 1231 649# CHECK: 650# CHECK: 7897 651 652# FIXME: handle overflowing nicely 653# overflow behaviour depends on 32 vs 64 bit 654 655# count here is isize::MAX 656# we store what to print as usize, so this will overflow 657# but we limit it to less than whatever the overflow is 658# so this should be fine 659# string repeat -m1 -n 9223372036854775807 aa 660# DONTCHECK: a 661 662# count is here (i64::MAX + 1) / 2 663# we end up overflowing, and the result is 0 664# but this should work fine, as we limit it way before the overflow 665# string repeat -m1 -n 4611686018427387904 aaaa 666# DONTCHECK: a 667 668# Historical string repeat behavior is no newline if no output. 669echo -n before 670string repeat -n5 '' 671echo after 672# CHECK: beforeafter 673 674string repeat -n-1 foo;and echo "exit 0" 675# CHECKERR: string repeat: Invalid count value '-1' 676 677string repeat -m-1 foo;and echo "exit 0" 678# CHECKERR: string repeat: Invalid max value '-1' 679 680string repeat -n notanumber foo;and echo "exit 0" 681# CHECKERR: string repeat: notanumber: invalid integer 682 683string repeat -m notanumber foo;and echo "exit 0" 684# CHECKERR: string repeat: notanumber: invalid integer 685 686echo stdin| string repeat -n1"and arg" ;and echo "exit 0" 687# CHECKERR: string repeat: too many arguments 688 689string repeat -n;and echo "exit 0" 690# CHECKERR: string repeat: -n: option requires an argument 691 692# FIXME: Also triggers usage 693# string repeat -l fakearg 694# DONTCHECKERR: string repeat: Unknown option '-l' 695 696string repeat"" 697# CHECKERR: string repeat: Invalid count value '' 698 699string repeat -n3"" 700or echo string repeat empty string failed 701# CHECK: string repeat empty string failed 702 703# See that we hit the expected length 704# First with "max", i.e. maximum number of characters 705string repeat -m5000 aab| string length 706# CHECK: 5000 707string repeat -m5000 ab| string length 708# CHECK: 5000 709string repeat -m5000 a| string length 710# CHECK: 5000 711string repeat -m17 aab| string length 712# CHECK: 17 713string repeat -m17 ab| string length 714# CHECK: 17 715string repeat -m17 a| string length 716# CHECK: 17 717# Then with "count", i.e. number of repetitions. 718# (these are count * length long) 719string repeat -n17 aab| string length 720# CHECK: 51 721string repeat -n17 ab| string length 722# CHECK: 34 723string repeat -n17 a| string length 724# CHECK: 17 725# And a more tricksy case with a long string that we truncate. 726string repeat -m5 ( string repeat -n500000 aaaaaaaaaaaaaaaaaa) | string length 727# CHECK: 5 728 729# might cause integer overflow 730string repeat -n2999 \n | count 731# CHECK: 3000 732 733# Test equivalent matches with/without the --entire, --regex, and --invert flags. 734string match -e x abc dxf xyz jkx x z 735or echo exit1 736# CHECK: dxf 737# CHECK: xyz 738# CHECK: jkx 739# CHECK: x 740 741string match x abc dxf xyz jkx x z 742# CHECK: x 743 744string match --entire -r"a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz 745or echo exit1 746# CHECK: abxc 747# CHECK: bye 748# CHECK: aaabyz 749# CHECK: kaabxz 750# CHECK: abbxy 751# CHECK: caabxyxz 752 753# 'string match --entire "" -- banana' 754string match --entire"" -- banana 755or echo exit1 756# CHECK: banana 757 758# 'string match -r "a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz' 759string match -r"a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz 760or echo exit1 761# CHECK: abx 762# CHECK: by 763# CHECK: aaaby 764# CHECK: aabx 765# CHECK: bxy 766# CHECK: aabxyx 767 768# Make sure that groups are handled correct with/without --entire. 769# 'string match --entire -r "a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz' 770string match --entire -r"a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz 771or echo exit1 772# CHECK: abxc 773# CHECK: x 774# CHECK: bye 775# CHECK: y 776# CHECK: aaabyz 777# CHECK: y 778# CHECK: kaabxz 779# CHECK: x 780# CHECK: abbxy 781# CHECK: xy 782# CHECK: caabxyxz 783# CHECK: xyx 784 785string match --entire --index foo foo 786# CHECKERR: string match: invalid option combination, --entire and --index are mutually exclusive 787 788string match --entire --groups-only -r foo foo 789# CHECKERR: string match: invalid option combination, --entire and --groups-only are mutually exclusive 790 791# 'string match -r "a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz' 792string match -r"a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz 793or echo exit1 794# CHECK: abx 795# CHECK: x 796# CHECK: by 797# CHECK: y 798# CHECK: aaaby 799# CHECK: y 800# CHECK: aabx 801# CHECK: x 802# CHECK: bxy 803# CHECK: xy 804# CHECK: aabxyx 805# CHECK: xyx 806 807# Test `string lower` and `string upper`. 808set x( string lower abc DEF gHi) 809or echo string lower exit1 810test $x [ 1 ] = abc-a $x [ 2 ] = def-a $x [ 3 ] = ghi 811or echo strings not converted to lowercase 812 813set x( echo abc DEF gHi| string lower) 814or echo string lower exit1 815test $x [ 1 ] = 'abc def ghi' 816or echo strings not converted to lowercase 817 818string lower -q abc 819and echo lowercasing a lowercase string did not fail as expected 820 821set x( string upper abc DEF gHi) 822or echo string upper exit1 823test $x [ 1 ] = ABC-a $x [ 2 ] = DEF-a $x [ 3 ] = GHI 824or echo strings not converted to uppercase 825 826set x( echo abc DEF gHi| string upper) 827or echo string upper exit1 828test $x [ 1 ] = 'ABC DEF GHI' 829or echo strings not converted to uppercase 830 831string upper -q ABC DEF 832and echo uppercasing a uppercase string did not fail as expected 833 834# 'Check NUL' 835# Note: We do `string escape` at the end to make a `\0` literal visible. 836printf 'a\0b\n' | string escape 837printf 'a\0c\n' | string match -e a| string escape 838printf 'a\0d\n' | string split'' | string escape 839printf 'a\0b\n' | string match -r'.*b$' | string escape 840printf 'a\0b\n' | string replace b g| string escape 841printf 'a\0b\n' | string replace -r b g| string escape 842# TODO: These do not yet work! 843# printf 'a\0b' | string match '*b' | string escape 844# CHECK: a\x00b 845# CHECK: a\x00c 846# CHECK: a 847# CHECK: \x00 848# CHECK: d 849# CHECK: a\x00b 850# CHECK: a\x00g 851# CHECK: a\x00g 852 853# string split0 854count ( echo -ne'abcdefghi' | string split0) 855# CHECK: 1 856count ( echo -ne'abc\x00def\x00ghi\x00' | string split0) 857# CHECK: 3 858count ( echo -ne'abc\x00def\x00ghi\x00\x00' | string split0) 859# CHECK: 4 860count ( echo -ne'abc\x00def\x00ghi' | string split0) 861# CHECK: 3 862count ( echo -ne'abc\ndef\x00ghi\x00' | string split0) 863# CHECK: 2 864count ( echo -ne'abc\ndef\nghi' | string split0) 865# CHECK: 1 866# #5701 - split0 always returned 1 867echo -ne'a\x00b' | string split0 868and echo Split something 869# CHECK: a 870# CHECK: b 871# CHECK: Split something 872 873# string join0 874set tmp beta alpha\n gamma 875count ( string join\n $tmp ) 876# CHECK: 3 877count ( string join0$tmp ) 878# CHECK: 2 879count ( string join0$tmp | string split0) 880# CHECK: 2 881 882# string split0 in functions 883# This function outputs some newline-separated content, and some 884# explicitly separated content. 885function dualsplit 886echo alpha 887echo beta 888echo -ne'gamma\x00delta' | string split0 889end 890count ( dualsplit ) 891# CHECK: 4 892 893# Ensure we handle empty outputs correctly (#5987) 894count ( string split / /) 895# CHECK: 2 896count ( echo -ne'\x00\x00\x00' | string split0) 897# CHECK: 3 898 899# string collect 900count ( echo one\n two\n three\n four| string collect) 901count ( echo one| string collect) 902# CHECK: 1 903# CHECK: 1 904echo [( echo one\n two\n three| string collect)] 905# CHECK: [one 906# CHECK: two 907# CHECK: three] 908echo [( echo one\n two\n three| string collect -N)] 909# CHECK: [one 910# CHECK: two 911# CHECK: three 912# CHECK: ] 913printf '[%s]\n' ( string collect one\n\n two\n ) 914# CHECK: [one] 915# CHECK: [two] 916printf '[%s]\n' ( string collect -N one\n\n two\n ) 917# CHECK: [one 918# CHECK: 919# CHECK: ] 920# CHECK: [two 921# CHECK: ] 922printf '[%s]\n' ( string collect --no-trim-newlines one\n\n two\n ) 923# CHECK: [one 924# CHECK: 925# CHECK: ] 926# CHECK: [two 927# CHECK: ] 928# string collect returns 0 when it has any output, otherwise 1 929string collect> /dev/null;and echo unexpected success;or echo expected failure 930# CHECK: expected failure 931echo -n| string collect> /dev/null;and echo unexpected success;or echo expected failure 932# CHECK: expected failure 933echo | string collect -N> /dev/null;and echo expected success;or echo unexpected failure 934# CHECK: expected success 935echo | string collect> /dev/null;and echo unexpected success;or echo expected failure 936# CHECK: expected failure 937string collect a> /dev/null;and echo expected success;or echo unexpected failure 938# CHECK: expected success 939string collect -N'' > /dev/null;and echo unexpected success;or echo expected failure 940# CHECK: expected failure 941string collect\n\n > /dev/null;and echo unexpected success;or echo expected failure 942# CHECK: expected failure 943 944echo "foo" ( true | string collect --allow-empty) "bar" 945# CHECK: foobar 946test -z ( string collect) 947and echo Nothing 948# CHECK: Nothing 949test -n ( string collect) 950and echo Something 951# CHECK: Something 952test -n ( string collect -a) 953or echo No, actually nothing 954# CHECK: No, actually nothing 955 956# string collect in functions 957# This function outputs some newline-separated content, and some 958# explicitly un-separated content. 959function dualcollect 960echo alpha 961echo beta 962echo gamma\n delta\n omega| string collect 963end 964count ( dualcollect ) 965# CHECK: 3 966 967string match -qer asd asd 968echo $status 969# CHECK: 0 970 971# should not be able to enable UTF mode 972string match -r"(*UTF).*" aaa 973# CHECKERR: string match: Regular expression compile error: using UTF is disabled by the application 974# CHECKERR: string match: (*UTF).* 975# CHECKERR: string match: ^ 976 977string replace -r"(*UTF).*" aaa 978# CHECKERR: string replace: Regular expression compile error: using UTF is disabled by the application 979# CHECKERR: string replace: (*UTF).* 980# CHECKERR: string replace: ^ 981 982string match -eq asd asd 983echo $status 984# CHECK: 0 985 986# Unmatched capturing groups are treated as empty 987echo az| string replace -r --'a(b.+)?z' 'a:$1z' 988# CHECK: a:z 989 990# --quiet should quit early 991echo "Checking that --quiet quits early - if this is broken it hangs" 992# CHECK: Checking that --quiet quits early - if this is broken it hangs 993yes | string match -q y 994echo $status 995# CHECK: 0 996yes | string length -q 997echo $status 998# CHECK: 0 999yes | string replace -q y n 1000echo $status 1001# CHECK: 0 1002 1003# `string` can't be wrapped properly anymore, since `string match` creates variables: 1004function string 1005builtin string$argv 1006end 1007# CHECKERR: {{.*}}checks/string.fish (line {{\d+}}): function: string: cannot use reserved keyword as function name 1008# CHECKERR: function string 1009# CHECKERR: ^~~~~~~~~~~~~~^ 1010 1011string escape\x7F 1012# CHECK: \x7f 1013 1014# This used to crash. 1015string pad -w8 he\e h 1016# CHECK: he 1017# CHECK: {{\x1bh}} 1018 1019string match -rg'(.*)fish' catfish 1020# CHECK: cat 1021string match -rg'(.*)fish' shellfish 1022# CHECK: shell 1023# An empty match 1024string match -rg'(.*)fish' fish 1025# No match at all 1026string match -rg'(.*)fish' banana 1027# Make sure it doesn't start matching something 1028string match -r --groups-only'(.+)fish' fish 1029echo $status 1030# CHECK: 1 1031# Multiple groups 1032string match -r --groups-only'(.+)fish(.*)' catfishcolor 1033# CHECK: cat 1034# CHECK: color 1035 1036# Examples specifically called out in #6056. 1037echo "foo bar baz" | string match -rg'foo (bar) baz' 1038# CHECK: bar 1039echo "foo1x foo2x foo3x" | string match -arg'foo(\d)x' 1040# CHECK: 1 1041# CHECK: 2 1042# CHECK: 3 1043 1044# Most subcommands preserve missing newline (#3847). 1045echo -n abc| string upper 1046echo '<eol>' 1047# CHECK: ABC<eol> 1048 1049# newline should not appear from nowhere when command does not split on newline 1050echo -n abc| string collect 1051echo '<eol>' 1052# CHECK: abc<eol> 1053 1054printf \< 1055printf my-password| string replace -ra .\* 1056printf \>\n 1057# CHECK: <***********> 1058 1059string shorten -m3 foo 1060# CHECK: foo 1061string shorten -m2 foo 1062# CHECK: f… 1063 1064string shorten -m5 foobar 1065# CHECK: foob… 1066 1067# Should produce no output and return false because there was nothing to shorten. 1068string shorten -m2 -q12 1069echo $status 1070# CHECK: 1 1071string shorten -lm2 -q12 1072echo $status 1073# CHECK: 1 1074 1075# Char is longer than width, we truncate instead. 1076string shorten -m5 --char ........ foobar 1077# CHECK: fooba 1078 1079string shorten --max4 -c /// foobar 1080# CHECK: f/// 1081 1082string shorten --max4 -c /// foobarnana 1083# CHECK: f/// 1084 1085string shorten --max2 --char"" foo 1086# CHECK: fo 1087 1088string shorten --max=-1 --char"" foo 1089# CHECKERR: string shorten: Invalid max value '-1' 1090 1091string shorten foo foobar 1092# CHECK: foo 1093# CHECK: fo… 1094 1095# pad with a bell, it has zero width, that's fine 1096string shorten -c\a foo foobar| string escape 1097# CHECK: foo 1098# CHECK: foo\cg 1099 1100string shorten -c\a w foo foobar| string escape 1101# CHECK: foo 1102# CHECK: fo\cgw 1103 1104# backspace is fine! 1105string shorten -c\b foo foobar| string escape 1106# CHECK: foo 1107# CHECK: foo\b 1108 1109string shorten -c\b a foo foobar| string escape 1110# CHECK: foo 1111# CHECK: fo\ba 1112 1113string shorten -c cool\b\b\b\b foo foobar| string escape 1114# CHECK: foo 1115# CHECK: foocool\b\b\b\b 1116 1117string shorten -c cool\b\b\b\b\b foo foobar| string escape 1118# CHECK: foo 1119# negative width ellipsis is fine 1120# CHECK: foocool\b\b\b\b\b 1121 1122string shorten -c\a\a XX foo foobar| string escape 1123# CHECK: foo 1124# CHECK: f\cg\cgXX 1125 1126# A weird case - our minimum width here is 1, 1127# so everything that goes over the width becomes "x" 1128for iin ( seq 1 10 ) 1129math 2 ^$i 1130end | string shorten -c x 1131# CHECK: 2 1132# CHECK: 4 1133# CHECK: 8 1134# CHECK: x 1135# CHECK: x 1136# CHECK: x 1137# CHECK: x 1138# CHECK: x 1139# CHECK: x 1140# CHECK: x 1141 1142string shorten -N -cx bar\n fooo 1143# CHECK: barx 1144 1145# Shorten and emoji width. 1146begin 1147# \U1F4A9 was widened in unicode 9, so it's affected 1148# by $fish_emoji_width 1149# "…" isn't and always has width 1. 1150# 1151# "abcde" has width 5, we have a total width of 6, 1152# so we need to overwrite the "e" with our ellipsis. 1153fish_emoji_width=1 string shorten --max=5 -- abcde💩 1154# CHECK: abcd… 1155# This fits assuming the poo fits in one column 1156fish_emoji_width=1 string shorten --max=6 -- abcde💩 1157# CHECK: abcde💩 1158 1159# This has a total width of 7 (assuming double-wide poo), 1160# so we need to add the ellipsis on the "e" 1161fish_emoji_width=2 string shorten --max=5 -- abcde💩 1162# CHECK: abcd… 1163# This still doesn't fit! 1164fish_emoji_width=2 string shorten --max=6 -- abcde💩 1165# CHECK: abcde… 1166fish_emoji_width=2 string shorten --max=7 -- abcde💩 1167# CHECK: abcde💩 1168end 1169 1170# See that colors aren't counted 1171string shorten -m6( set_color blue) s( set_color red) t( set_color --bold brwhite) rin( set_color red) g( set_color yellow) -shorten| string escape 1172# Renders like "strin…" in colors 1173# Note that red sequence that we still pass on because it's width 0. 1174# CHECK: \e\[34ms\e\[31mt\e\[97\;1mrin\e\[31m… 1175 1176# See that colors aren't counted in ellipsis 1177string shorten -c( set_color blue) s( set_color red) t( set_color --bold brwhite) rin( set_color red) g -m8 abcdefghijklmno| string escape 1178# Renders like "abstring" in colors 1179# CHECK: ab\e\[34ms\e\[31mt\e\[97\;1mrin\e\[31mg 1180 1181set -l str( set_color blue) s( set_color red) t( set_color --bold brwhite) rin( set_color red) g( set_color yellow) -shorten 1182for iin ( seq 1 ( string length -V --$str )) 1183set -l len( string shorten -m$i --$str | string length -V) 1184test $len = $i 1185or echo Oopsie ellipsizing to$i failed 1186end 1187 1188string shorten -m4 foobar\n bananarama 1189# CHECK: foo… 1190# CHECK: ban… 1191 1192# First line is empty and printed as-is 1193# The other lines are truncated to the width of the first real line. 1194printf ' 11951. line 11962. another line 11973. third line' | string shorten 1198# CHECK: 1199# CHECK: 1. line 1200# CHECK: 2. ano… 1201# CHECK: 3. thi… 1202 1203printf ' 12041. line 12052. another line 12063. third line' | string shorten --left 1207# CHECK: 1208# CHECK: 1. line 1209# CHECK: …r line 1210# CHECK: …d line 1211 1212string shorten -m12 -l( set_color blue) s( set_color red) t( set_color --bold brwhite) rin( set_color red)( set_color green) g( set_color yellow) -shorten| string escape 1213# Renders like "…ing-shorten" with g in green and "-shorten" in yellow 1214# Yes, that's a "red" escape before. 1215# CHECK: …in\e\[31m\e\[32mg\e\[33m-shorten 1216 1217set -l str( set_color blue) s( set_color red) t( set_color --bold brwhite) rin( set_color red) g( set_color yellow) -shorten 1218for iin ( seq 1 ( string length -V --$str )) 1219set -l len( string shorten -m$i --left --$str | string length -V) 1220test $len = $i 1221or echo Oopsie ellipsizing to$i failed 1222end 1223 1224string shorten -m0 foo bar asodjsaoidj 1225# CHECK: foo 1226# CHECK: bar 1227# CHECK: asodjsaoidj 1228 1229# backspaces are weird 1230string shorten abc ab abcdef( string repeat -n6 \b ) | string escape 1231# CHECK: a… 1232# CHECK: ab 1233# this line has length zero, since backspace removes it all 1234# CHECK: abcdef\b\b\b\b\b\b 1235 1236# due to an integer overflow this might truncate the third backspaced one, it should not 1237string shorten abc ab abcdef( string repeat -n7 \b ) | string escape 1238# CHECK: a… 1239# CHECK: ab 1240# this line has length zero, since backspace removes it all 1241# CHECK: abcdef\b\b\b\b\b\b\b 1242 1243# due to an integer overflow this might truncate 1244string shorten abc\b ab ab abcdef| string escape 1245# CHECK: a… 1246# backspace does not contribute length at the start 1247# CHECK: \bab 1248# CHECK: ab 1249# CHECK: a… 1250 1251string shorten abc\b abc ab abcdef| string escape 1252# CHECK: a… 1253# CHECK: \ba… 1254# CHECK: ab 1255# CHECK: a… 1256 1257# non-printable-escape-chars (in this case bell) 1258string shorten abc ab abcdef( string repeat -n6 \a ) | string escape 1259# CHECK: a… 1260# CHECK: ab 1261# CHECK: a… 1262 1263string shorten abc ab abcdef( string repeat -n7 \a ) | string escape 1264# CHECK: a… 1265# CHECK: ab 1266# CHECK: a… 1267 1268string shorten abc\a ab ab abcdef| string escape 1269# CHECK: a… 1270# non-printables have length 0 1271# CHECK: \cgab 1272# CHECK: ab 1273# CHECK: a… 1274 1275string shorten abc\a abc ab abcdef| string escape 1276# CHECK: a… 1277# CHECK: \cga… 1278# CHECK: ab 1279# CHECK: a… 1280 1281printf "dog\ncat\nbat\ngnat\n" | string match -m2"*at" 1282# CHECK: cat 1283# CHECK: bat 1284 1285string match -m0 foo 1286# CHECKERR: string match: Invalid max matches value '0' 1287 1288string match -m999999999999999999999999999999999999999 foo 1289# CHECKERR: string match: Invalid max matches value '999999999999999999999999999999999999999' 1290 1291printf "dog\ncat\nbat\nhog\n" | string match -rvm1'at$' 1292# CHECK: dog 1293 1294printf "dog\ncat\nbat\n" | string replace -rf --max-matches1 'at$' aught 1295# CHECK: caught 1296 1297printf "dog\ncat\nbat\n" | string replace -r --max-matches1 '^c' h 1298# CHECK: dog 1299# CHECK: hat 1300 1301$fish --features="no-regex-easyesc" -c"string replace -r o '\c' -- foo" 1302# CHECKERR: string replace: Invalid escape sequence in pattern "\c" 1303 1304string replace --max-matches abc 1305# CHECKERR: string replace: Invalid max matches value 'abc' 1306string replace --max-matches-1 1307# CHECKERR: string replace: Invalid max matches value '-1' 1308string replace --max-matches99999999999999999999 1309# CHECKERR: string replace: Invalid max matches value '99999999999999999999' 1310 1311string replace 1312# CHECKERR: string replace: missing argument 1313string replace one 1314# CHECKERR: string replace: expected 2 arguments; got 1 1315 1316string replace -r o'${bad_name}' foobar 1317# CHECKERR: string replace: Regular expression substitute error: unknown substring 1318 1319string match --unknown-opt 1320# CHECKERR: string match: --unknown-opt: unknown option 1321# CHECKERR: {{.*}}checks/string.fish (line {{\d+}}): 1322# CHECKERR: string match --unknown-opt 1323# CHECKERR: ^ 1324# CHECKERR: (Type 'help string' for related documentation) 1325 1326string match --regex=abc 1327# CHECKERR: string match: --regex=abc: option does not take an argument