src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_str_test.elvts (about)

     1  /////////////////////
     2  # string comparison #
     3  /////////////////////
     4  
     5  ~> <s a b
     6  ▶ $true
     7  ~> <s 2 10
     8  ▶ $false
     9  ~> <s a b c
    10  ▶ $true
    11  ~> <=s a a
    12  ▶ $true
    13  ~> <=s a b
    14  ▶ $true
    15  ~> <=s b a
    16  ▶ $false
    17  ~> <=s a a b
    18  ▶ $true
    19  ~> ==s haha haha
    20  ▶ $true
    21  ~> ==s 10 10.0
    22  ▶ $false
    23  ~> ==s a a a
    24  ▶ $true
    25  ~> >s a b
    26  ▶ $false
    27  ~> >s 2 10
    28  ▶ $true
    29  ~> >s c b a
    30  ▶ $true
    31  ~> >=s a a
    32  ▶ $true
    33  ~> >=s a b
    34  ▶ $false
    35  ~> >=s b a
    36  ▶ $true
    37  ~> >=s b a a
    38  ▶ $true
    39  ~> !=s haha haha
    40  ▶ $false
    41  ~> !=s 10 10.1
    42  ▶ $true
    43  // !=s only accepts two arguments
    44  ~> !=s a b a
    45  Exception: arity mismatch: arguments must be 2 values, but is 3 values
    46    [tty]:1:1-9: !=s a b a
    47  
    48  /////////////
    49  # to-string #
    50  /////////////
    51  
    52  ~> to-string str (num 1) $true
    53  ▶ str
    54  ▶ 1
    55  ▶ '$true'
    56  // bubbling output errors
    57  ~> to-string str >&-
    58  Exception: port does not support value output
    59    [tty]:1:1-17: to-string str >&-
    60  
    61  ////////
    62  # base #
    63  ////////
    64  
    65  ~> base 2 1 3 4 16 255
    66  ▶ 1
    67  ▶ 11
    68  ▶ 100
    69  ▶ 10000
    70  ▶ 11111111
    71  ~> base 16 42 233
    72  ▶ 2a
    73  ▶ e9
    74  // *big.Int
    75  ~> base 16 100000000000000000000
    76  ▶ 56bc75e2d63100000
    77  ~> base 10 0x56bc75e2d63100000
    78  ▶ 100000000000000000000
    79  // float64 storing an integer
    80  ~> base 16 256.0
    81  ▶ 100
    82  // float64 storing an integer that doesn't fit in int64
    83  ~> base 16 100000000000000000000.0
    84  ▶ 56bc75e2d63100000
    85  // typed number as arguments
    86  ~> base (num 16) (num 256)
    87  ▶ 100
    88  // bad arguments
    89  ~> base 16 1.2
    90  Exception: bad value: number must be integer, but is (num 1.2)
    91    [tty]:1:1-11: base 16 1.2
    92  ~> base 8 1/8
    93  Exception: bad value: number must be integer, but is (num 1/8)
    94    [tty]:1:1-10: base 8 1/8
    95  ~> base 1 1
    96  Exception: out of range: base must be from 2 to 36, but is 1
    97    [tty]:1:1-8: base 1 1
    98  ~> base 37 10
    99  Exception: out of range: base must be from 2 to 36, but is 37
   100    [tty]:1:1-10: base 37 10
   101  // bubbling output error
   102  ~> base 2 1 >&-
   103  Exception: port does not support value output
   104    [tty]:1:1-12: base 2 1 >&-
   105  
   106  ////////////
   107  # wcswidth #
   108  ////////////
   109  
   110  ~> wcswidth 你好
   111  ▶ (num 4)
   112  ~> -override-wcwidth x 10; wcswidth 1x2x; -override-wcwidth x 1
   113  ▶ (num 22)
   114  
   115  ////////
   116  # eawk #
   117  ////////
   118  
   119  //each:with-deprecation-level 19
   120  
   121  ~> echo "  ax  by cz  \n11\t22 33" | eawk {|@a| put $a[-1] }
   122  ▶ cz
   123  ▶ 33
   124  
   125  ## bad input type ##
   126  ~> num 42 | eawk {|@a| fail "this should not run" }
   127  Exception: input of eawk must be string
   128    [tty]:1:10-48: num 42 | eawk {|@a| fail "this should not run" }
   129  
   130  ## propagating exception ##
   131  ~> to-lines [1 2 3 4] | eawk {|@a|
   132         if (==s 3 $a[1]) {
   133             fail "stop eawk"
   134         }
   135         put $a[1]
   136     }
   137  ▶ 1
   138  ▶ 2
   139  Exception: stop eawk
   140    [tty]:3:9-24:         fail "stop eawk"
   141    [tty]:1:22-6:1:
   142      to-lines [1 2 3 4] | eawk {|@a|
   143          if (==s 3 $a[1]) {
   144              fail "stop eawk"
   145          }
   146          put $a[1]
   147      }
   148  
   149  ## break ##
   150  ~> to-lines [" a" "b\tc " "d" "e"] | eawk {|@a|
   151         if (==s d $a[1]) {
   152             break
   153         } else {
   154             put $a[-1]
   155         }
   156     }
   157  ▶ a
   158  ▶ c
   159  
   160  ## continue ##
   161  ~> to-lines [" a" "b\tc " "d" "e"] | eawk {|@a|
   162         if (==s d $a[1]) {
   163             continue
   164         } else {
   165             put $a[-1]
   166         }
   167     }
   168  ▶ a
   169  ▶ c
   170  ▶ e
   171  
   172  ## parsing docker image ls output with custom separator ##
   173  ~> to-lines [
   174         'REPOSITORY                TAG          IMAGE ID      CREATED         SIZE'
   175         '<none>                    <none>       265c2d25a944  16 minutes ago  67.5 MB'
   176         '<none>                    <none>       26408a88b236  16 minutes ago  389 MB'
   177         'localhost/elvish_eawk     latest       0570db4e3eaa  32 hours ago    67.5 MB'
   178         'localhost/elvish          latest       59b1eec93ab7  33 hours ago    67.5 MB'
   179         'docker.io/library/golang  latest       015e6b7f599b  46 hours ago    838 MB'
   180         'docker.io/library/golang  1.20-alpine  93db368a0a9e  3 days ago      266 MB'
   181     ] | eawk &sep=" [ ]+" {|0 1 2 3 4 5| put $5 }
   182  ▶ SIZE
   183  ▶ '67.5 MB'
   184  ▶ '389 MB'
   185  ▶ '67.5 MB'
   186  ▶ '67.5 MB'
   187  ▶ '838 MB'
   188  ▶ '266 MB'