| 1 | ---
|
| 2 | title: Builtin Functions (Oils Reference)
|
| 3 | all_docs_url: ..
|
| 4 | body_css_class: width40
|
| 5 | default_highlighter: oils-sh
|
| 6 | preserve_anchor_case: yes
|
| 7 | ---
|
| 8 |
|
| 9 | <div class="doc-ref-header">
|
| 10 |
|
| 11 | [Oils Reference](index.html) —
|
| 12 | Chapter **Builtin Functions**
|
| 13 |
|
| 14 | </div>
|
| 15 |
|
| 16 | This chapter describes builtin functions (as opposed to [builtin
|
| 17 | commands](chap-builtin-cmd.html).)
|
| 18 |
|
| 19 | <span class="in-progress">(in progress)</span>
|
| 20 |
|
| 21 | <div id="dense-toc">
|
| 22 | </div>
|
| 23 |
|
| 24 | ## Values
|
| 25 |
|
| 26 | ### len()
|
| 27 |
|
| 28 | Returns the
|
| 29 |
|
| 30 | - number of entries in a `List`
|
| 31 | - number of pairs in a `Dict`
|
| 32 | - number of bytes in a `Str`
|
| 33 | - TODO: `countRunes()` can return the number of UTF-8 encoded code points.
|
| 34 |
|
| 35 | ### func/type()
|
| 36 |
|
| 37 | Given an arbitrary value, returns a string representing the value's runtime
|
| 38 | type.
|
| 39 |
|
| 40 | For example:
|
| 41 |
|
| 42 | var d = {'foo': 'bar'}
|
| 43 | var n = 1337
|
| 44 |
|
| 45 | $ = type(d)
|
| 46 | (Str) 'Dict'
|
| 47 |
|
| 48 | $ = type(n)
|
| 49 | (Str) 'Int'
|
| 50 |
|
| 51 | Similar names: [type][]
|
| 52 |
|
| 53 | [type]: chap-index.html#type
|
| 54 |
|
| 55 |
|
| 56 | ## Conversions
|
| 57 |
|
| 58 | ### bool()
|
| 59 |
|
| 60 | Returns the truth value of its argument. Similar to `bool()` in python, it
|
| 61 | returns `false` for:
|
| 62 |
|
| 63 | - `false`, `0`, `0.0`, `''`, `{}`, `[]`, and `null`.
|
| 64 |
|
| 65 | Returns `true` for all other values.
|
| 66 |
|
| 67 | ### int()
|
| 68 |
|
| 69 | Given a float, returns the largest integer that is less than its argument (i.e. `floor()`).
|
| 70 |
|
| 71 | $ = int(1.99)
|
| 72 | (Int) 1
|
| 73 |
|
| 74 | Given a string, `Int()` will attempt to convert the string to a base-10
|
| 75 | integer. The base can be overridden by calling with a second argument.
|
| 76 |
|
| 77 | $ = int('10')
|
| 78 | (Int) 10
|
| 79 |
|
| 80 | $ = int('10', 2)
|
| 81 | (Int) 2
|
| 82 |
|
| 83 | ysh$ = Int('foo')
|
| 84 | # fails with an expression error
|
| 85 |
|
| 86 | ### float()
|
| 87 |
|
| 88 | Given an integer, returns the corresponding floating point representation.
|
| 89 |
|
| 90 | $ = float(1)
|
| 91 | (Float) 1.0
|
| 92 |
|
| 93 | Given a string, `Float()` will attempt to convert the string to float.
|
| 94 |
|
| 95 | $ = float('1.23')
|
| 96 | (Float) 1.23
|
| 97 |
|
| 98 | ysh$ = float('bar')
|
| 99 | # fails with an expression error
|
| 100 |
|
| 101 | ### str()
|
| 102 |
|
| 103 | Converts a `Float` or `Int` to a string.
|
| 104 |
|
| 105 | ### list()
|
| 106 |
|
| 107 | Given a list, returns a shallow copy of the original.
|
| 108 |
|
| 109 | Given an iterable value (e.g. a range or dictionary), returns a list containing
|
| 110 | one element for each item in the original collection.
|
| 111 |
|
| 112 | $ = list({'a': 1, 'b': 2})
|
| 113 | (List) ['a', 'b']
|
| 114 |
|
| 115 | $ = list(1:5)
|
| 116 | (List) [1, 2, 3, 4, 5]
|
| 117 |
|
| 118 | ### dict()
|
| 119 |
|
| 120 | Given a dictionary, returns a shallow copy of the original.
|
| 121 |
|
| 122 | ### runes()
|
| 123 |
|
| 124 | TODO
|
| 125 |
|
| 126 | Given a string, decodes UTF-8 into a List of integer "runes" (aka code points).
|
| 127 |
|
| 128 | Each rune is in the range `U+0` to `U+110000`, and **excludes** the surrogate
|
| 129 | range.
|
| 130 |
|
| 131 | runes(s, start=-1, end=-1)
|
| 132 |
|
| 133 | TODO: How do we signal errors?
|
| 134 |
|
| 135 | (`runes()` can be used to implement implemented Python's `ord()`.)
|
| 136 |
|
| 137 | ### encodeRunes()
|
| 138 |
|
| 139 | TODO
|
| 140 |
|
| 141 | Given a List of integer "runes" (aka code points), return a string.
|
| 142 |
|
| 143 | (`encodeRunes()` can be used to implement implemented Python's `chr()`.)
|
| 144 |
|
| 145 | ### bytes()
|
| 146 |
|
| 147 | TODO
|
| 148 |
|
| 149 | Given a string, return a List of integer byte values.
|
| 150 |
|
| 151 | Each byte is in the range 0 to 255.
|
| 152 |
|
| 153 | ### encodeBytes()
|
| 154 |
|
| 155 | TODO
|
| 156 |
|
| 157 | Given a List of integer byte values, return a string.
|
| 158 |
|
| 159 | ## Str
|
| 160 |
|
| 161 | ### strcmp()
|
| 162 |
|
| 163 | TODO
|
| 164 |
|
| 165 | ### shSplit()
|
| 166 |
|
| 167 | Split a string into a List of strings, using the shell algorithm that respects
|
| 168 | `$IFS`.
|
| 169 |
|
| 170 | Prefer `split()` to `shSplit()`.
|
| 171 |
|
| 172 |
|
| 173 | ## List
|
| 174 |
|
| 175 | ### join()
|
| 176 |
|
| 177 | Given a List, stringify its items, and join them by a separator. The default
|
| 178 | separator is the empty string.
|
| 179 |
|
| 180 | var x = ['a', 'b', 'c']
|
| 181 |
|
| 182 | $ echo $[join(x)]
|
| 183 | abc
|
| 184 |
|
| 185 | $ echo $[join(x, ' ')] # optional separator
|
| 186 | a b c
|
| 187 |
|
| 188 |
|
| 189 | It's also often called with the `=>` chaining operator:
|
| 190 |
|
| 191 | var items = [1, 2, 3]
|
| 192 |
|
| 193 | json write (items => join()) # => "123"
|
| 194 | json write (items => join(' ')) # => "1 2 3"
|
| 195 | json write (items => join(', ')) # => "1, 2, 3"
|
| 196 |
|
| 197 | ## Dict
|
| 198 |
|
| 199 | ### keys()
|
| 200 |
|
| 201 | Returns all existing keys from a dict as a list of strings.
|
| 202 |
|
| 203 | var en2fr = {
|
| 204 | hello: "bonjour",
|
| 205 | friend: "ami",
|
| 206 | cat: "chat"
|
| 207 | }
|
| 208 | = keys(en2fr)
|
| 209 | # => (List 0x4689) ["hello","friend","cat"]
|
| 210 |
|
| 211 | ### values()
|
| 212 |
|
| 213 | Similar to `keys()`, but returns the values of the dictionary.
|
| 214 |
|
| 215 | var person = {
|
| 216 | name: "Foo",
|
| 217 | age: 25,
|
| 218 | hobbies: :|walking reading|
|
| 219 | }
|
| 220 | = values(en2fr)
|
| 221 | # => (List 0x4689) ["Foo",25,["walking","reading"]]
|
| 222 |
|
| 223 | ### get()
|
| 224 |
|
| 225 | Return value for given key, falling back to the default value if the key
|
| 226 | doesn't exist.
|
| 227 |
|
| 228 | var book = {
|
| 229 | title: "Hitchhiker's Guide",
|
| 230 | published: 1979,
|
| 231 | }
|
| 232 |
|
| 233 | var published = get(book, 'published', null)
|
| 234 | = published
|
| 235 | # => (Int) 1979
|
| 236 |
|
| 237 | var author = get(book, 'author', "???")
|
| 238 | = author
|
| 239 | # => (Str) "???"
|
| 240 |
|
| 241 | If not specified, the default value is `null`:
|
| 242 |
|
| 243 | var author = get(book, 'author')
|
| 244 | = author
|
| 245 | # => (Null) null
|
| 246 |
|
| 247 | ## Float
|
| 248 |
|
| 249 | ### floatsEqual()
|
| 250 |
|
| 251 | Check if two floating point numbers are equal.
|
| 252 |
|
| 253 | = floatsEqual(42.0, 42.0)
|
| 254 | (Bool) true
|
| 255 |
|
| 256 | It's usually better to make an approximate comparison:
|
| 257 |
|
| 258 | = abs(float1 - float2) < 0.001
|
| 259 | (Bool) false
|
| 260 |
|
| 261 | ## Obj
|
| 262 |
|
| 263 | ### first()
|
| 264 |
|
| 265 | Get the Dict that contains an object's properties.
|
| 266 |
|
| 267 | ysh$ = first(obj)
|
| 268 | (Dict) {x: 42}
|
| 269 |
|
| 270 | The Dict and Obj share the same storage. So if the Dict is modified, the
|
| 271 | object is too.
|
| 272 |
|
| 273 | If you want a copy, use `dict(obj)`.
|
| 274 |
|
| 275 | ### rest()
|
| 276 |
|
| 277 | Get the "prototype" of an Obj, which is another Obj, or null:
|
| 278 |
|
| 279 | ysh$ = rest(obj)
|
| 280 | (Null) null
|
| 281 |
|
| 282 | ## Word
|
| 283 |
|
| 284 | ### glob()
|
| 285 |
|
| 286 | See `glob-pat` topic for syntax.
|
| 287 |
|
| 288 | ### maybe()
|
| 289 |
|
| 290 | ## Serialize
|
| 291 |
|
| 292 | ### toJson()
|
| 293 |
|
| 294 | Convert an object in memory to JSON text:
|
| 295 |
|
| 296 | $ = toJson({name: "alice"})
|
| 297 | (Str) '{"name":"alice"}'
|
| 298 |
|
| 299 | Add indentation by passing the `space` param:
|
| 300 |
|
| 301 | $ = toJson([42], space=2)
|
| 302 | (Str) "[\n 42\n]"
|
| 303 |
|
| 304 | Turn non-serializable types into `null`, instead of raising an error:
|
| 305 |
|
| 306 | $ = toJson(/d+/, type_errors=false)
|
| 307 | (Str) 'null'
|
| 308 |
|
| 309 | The `toJson()` function is to `json write (x)`, except the default value of
|
| 310 | `space` is 0.
|
| 311 |
|
| 312 | See [err-json-encode][] for errors.
|
| 313 |
|
| 314 | [err-json-encode]: chap-errors.html#err-json-encode
|
| 315 |
|
| 316 | ### fromJson()
|
| 317 |
|
| 318 | Convert JSON text to an object in memory:
|
| 319 |
|
| 320 | = fromJson('{"name":"alice"}')
|
| 321 | (Dict) {"name": "alice"}
|
| 322 |
|
| 323 | Similar to `json read <<< '{"name": "alice"}'`.
|
| 324 |
|
| 325 | See [err-json-decode][] for errors.
|
| 326 |
|
| 327 | [err-json-decode]: chap-errors.html#err-json-decode
|
| 328 |
|
| 329 | ### toJson8()
|
| 330 |
|
| 331 | Like `toJson()`, but it also converts binary data (non-Unicode strings) to
|
| 332 | J8-style `b'foo \yff'` strings.
|
| 333 |
|
| 334 | In contrast, `toJson()` will do a lossy conversion with the Unicode replacement
|
| 335 | character.
|
| 336 |
|
| 337 | See [err-json8-encode][] for errors.
|
| 338 |
|
| 339 | [err-json8-encode]: chap-errors.html#err-json8-encode
|
| 340 |
|
| 341 | ### fromJson8()
|
| 342 |
|
| 343 | Like `fromJson()`, but it also accepts binary data denoted by J8-style `b'foo
|
| 344 | \yff'` strings.
|
| 345 |
|
| 346 | See [err-json8-decode][] for errors.
|
| 347 |
|
| 348 | [err-json8-decode]: chap-errors.html#err-json8-decode
|
| 349 |
|
| 350 | ## Pattern
|
| 351 |
|
| 352 | ### `_group()`
|
| 353 |
|
| 354 | Like `Match => group()`, but accesses the global match created by `~`:
|
| 355 |
|
| 356 | if ('foo42' ~ / d+ /) {
|
| 357 | echo $[_group(0)] # => 42
|
| 358 | }
|
| 359 |
|
| 360 | ### `_start()`
|
| 361 |
|
| 362 | Like `Match => start()`, but accesses the global match created by `~`:
|
| 363 |
|
| 364 | if ('foo42' ~ / d+ /) {
|
| 365 | echo $[_start(0)] # => 3
|
| 366 | }
|
| 367 |
|
| 368 | ### `_end()`
|
| 369 |
|
| 370 | Like `Match => end()`, but accesses the global match created by `~`:
|
| 371 |
|
| 372 | if ('foo42' ~ / d+ /) {
|
| 373 | echo $[_end(0)] # => 5
|
| 374 | }
|
| 375 |
|
| 376 | ## Reflection
|
| 377 |
|
| 378 | ### func/eval()
|
| 379 |
|
| 380 | This function is like [`io->eval()`][io/eval], but it disallows I/O.
|
| 381 |
|
| 382 | Example:
|
| 383 |
|
| 384 | var cmd = ^(const x = 42)
|
| 385 | var d = eval(cmd, to_dict=true) # {x: 42}
|
| 386 |
|
| 387 | [io/eval]: chap-type-method.html#io/eval
|
| 388 |
|
| 389 | ### func/evalExpr()
|
| 390 |
|
| 391 | This function is like [`io->evalExpr()`][io/evalExpr], but it disallows I/O.
|
| 392 |
|
| 393 | Example:
|
| 394 |
|
| 395 | var x = 42
|
| 396 | var expr = ^[x + 1]
|
| 397 | var val = evalExpr(expr) # 43
|
| 398 |
|
| 399 | [io/evalExpr]: chap-type-method.html#io/evalExpr
|
| 400 |
|
| 401 | ## Introspect
|
| 402 |
|
| 403 | ### `shvarGet()`
|
| 404 |
|
| 405 | Given a variable name, return its value. It uses the "dynamic scope" rule,
|
| 406 | which looks up the stack for a variable.
|
| 407 |
|
| 408 | It's meant to be used with `shvar`:
|
| 409 |
|
| 410 | proc proc1 {
|
| 411 | shvar PATH=/tmp { # temporarily set PATH in this stack frame
|
| 412 | my-proc
|
| 413 | }
|
| 414 |
|
| 415 | proc2
|
| 416 | }
|
| 417 |
|
| 418 | proc proc2 {
|
| 419 | proc3
|
| 420 | }
|
| 421 |
|
| 422 | proc proc3 {
|
| 423 | var path = shvarGet('PATH') # Look up the stack (dynamic scoping)
|
| 424 | echo $path # => /tmp
|
| 425 | }
|
| 426 |
|
| 427 | proc1
|
| 428 |
|
| 429 | Note that `shvar` is usually for string variables, and is analogous to `shopt`
|
| 430 | for "booleans".
|
| 431 |
|
| 432 | If the variable isn't defined, `shvarGet()` returns `null`. So there's no way
|
| 433 | to distinguish an undefined variable from one that's `null`.
|
| 434 |
|
| 435 | ### `getVar()`
|
| 436 |
|
| 437 | Given a variable name, return its value.
|
| 438 |
|
| 439 | $ var x = 42
|
| 440 | $ echo $[getVar('x')]
|
| 441 | 42
|
| 442 |
|
| 443 | The variable may be local or global. (Compare with `shvarGet()`.) the "dynamic
|
| 444 | scope" rule.)
|
| 445 |
|
| 446 | If the variable isn't defined, `getVar()` returns `null`. So there's no way to
|
| 447 | distinguish an undefined variable from one that's `null`.
|
| 448 |
|
| 449 | ### `setVar()`
|
| 450 |
|
| 451 | Bind a name to a value, in the local scope. Returns nothing.
|
| 452 |
|
| 453 | call setVar('myname', 42)
|
| 454 |
|
| 455 | This is like
|
| 456 |
|
| 457 | setvar myname = 42
|
| 458 |
|
| 459 | except the name can is a string, which can be constructed at runtime.
|
| 460 |
|
| 461 | ---
|
| 462 |
|
| 463 | You can also bind globals:
|
| 464 |
|
| 465 | call setVar('myname', 42, global=true)
|
| 466 |
|
| 467 | which is like
|
| 468 |
|
| 469 | setglobal myname = 42
|
| 470 |
|
| 471 | ### `getShFunction`
|
| 472 |
|
| 473 | Given the name of a shell function, return the corresponding [Proc][] value, or
|
| 474 | `null` if it's not found.
|
| 475 |
|
| 476 | [Proc]: chap-type-method.html#Proc
|
| 477 |
|
| 478 | ### `parseCommand()`
|
| 479 |
|
| 480 | Given a code string, parse it as a command (with the current parse options).
|
| 481 |
|
| 482 | Returns a `value.Command` instance, or raises an error.
|
| 483 |
|
| 484 | ### `parseExpr()`
|
| 485 |
|
| 486 | TODO:
|
| 487 |
|
| 488 | Given a code string, parse it as an expression.
|
| 489 |
|
| 490 | Returns a `value.Expr` instance, or raises an error.
|
| 491 |
|
| 492 | ### `bindFrame()`
|
| 493 |
|
| 494 | TODO
|
| 495 |
|
| 496 | ## Hay Config
|
| 497 |
|
| 498 | ### parseHay()
|
| 499 |
|
| 500 | ### evalHay()
|
| 501 |
|
| 502 |
|
| 503 | ## Hashing
|
| 504 |
|
| 505 | ### sha1dc()
|
| 506 |
|
| 507 | Git's algorithm.
|
| 508 |
|
| 509 | ### sha256()
|
| 510 |
|
| 511 |
|
| 512 | <!--
|
| 513 |
|
| 514 | ### Better Syntax
|
| 515 |
|
| 516 | These functions give better syntax to existing shell constructs.
|
| 517 |
|
| 518 | - `shQuote()` for `printf %q` and `${x@Q}`
|
| 519 | - `trimLeft()` for `${x#prefix}` and `${x##prefix}`
|
| 520 | - `trimRight()` for `${x%suffix}` and `${x%%suffix}`
|
| 521 | - `trimLeftGlob()` and `trimRightGlob()` for slow, legacy glob
|
| 522 | - `upper()` for `${x^^}`
|
| 523 | - `lower()` for `${x,,}`
|
| 524 | - `strftime()`: hidden in `printf`
|
| 525 |
|
| 526 | -->
|