Strings

Types
  • string

    • Used as default when doing type inference: my_string := "hello" .

    • Stores the pointer to the data and the length of the string.

  • cstring

    • "A little longer, with a 0 at the end".

    • Is used to interface with foreign libraries written in/for C that use zero-terminated strings.

Syntax
"string"
'rune'
`multiline_string`

Manipulation

import "core:strings"
  • If there is allocation, delete  is used.

  • Compare :

    value: int = strings.compare("hello", "hi")
    
  • Contains :

    flag: bool = strings.contains("hello", "hi")  // "hi" is in "hello"
    
  • Concatenate :

    my_string, err := strings.concatenate({"hello", "hi"})
    defer delete(my_string)
    
  • Upper :

    my_string := strings.to_upper("hello")
    defer delete(my_string)
    
  • Lower :

    my_string := strings.to_lower("hello")
    defer delete(my_string)
    
  • Cut :

    • "substring", "make the string smaller".

    my_string, err := strings.cut("hello", 3, 5)  // (string, first_idx, last_idx)
    defer delete(my_string)
    

Slicing

  • Uses array slicing property.

my_str := "little cat"
sub_str := my_str[7:]
    // `cat`
  • Depending on the char, it is useful to use the "core:string"  library to avoid the issues below.

  • In the example below, ideally use "runes" instead of "bytes", since Japanese chars use 3 bytes per rune.

my_str := "imagine something Japanese"
sub_str := my_str[1:]
    // issues

Prints

Formatting
  • aprint .

    • aprintln

    • aprintf

    • aprintfln .

    • Takes any  and returns string .

  • tprint .

    • tprintln .

    • tprintf .

    • tprintfln .

    • Takes any  and returns int .

    • Prints  to os.stdout .

    • Allocates with the temp_allocator .

  • bprint .

    • bprintln .

    • bprintf .

    • bprintfln .

    • Takes []u8  and returns string .

  • sbprint .

    • sbprintln .

    • sbprintf .

    • sbprintfln .

    • Takes ^strings.Builder  and returns string .

  • caprint .

    • caprintln .

    • caprintf .

    • caprintfln .

    • Takes any  and returns cstring .

  • ctprint .

    • ctprintln .

    • ctprintf .

    • ctprintfln .

    • Takes any  and returns cstring .

    • Allocates with the temp_allocator .

Writes to Terminal (os.std)
  • fmt.print .

    • println .

    • printf .

    • printfln .

    • Takes any  and returns int .

    • Prints  to os.stdout .

  • fmt.eprint .

    • eprintln .

    • eprintf .

    • eprintfln .

    • Takes any  and returns int .

    • Prints  to os.stderr .

  • fmt.panicf .

    • Takes any  and returns void .

    • Panics .

Write to File
  • fprint .

    • fprintln .

    • fprintf .

    • fprintfln .

    • fprint_type .

    • fprint_typeid .

    • Takes os.Handle  and returns int .

    • Writes  to file.

Writes to io.Stream
  • wprint .

    • wprintln .

    • wprintf .

    • wprintfln .

    • wprint_type .

    • wprint_typeid .

    • Takes io.Stream  and returns int  (bytes written).

    • Writes  to stream.

Formatting

Pretty Formatting
  • Opt 1 :

     fmt.printf(
        "Ping %d:\n" +
        "  Client RTT: %vms (self-measured)\n" +
        "  Server RTT: %vms (server's view of us)\n",
        i,
        time.duration_milliseconds(client_rtt),
        time.duration_milliseconds(pong.client_ping), // Server's estimate
    )
    
  • Opt 2 :

    • core:text/table .

      • Examples:

      A_LONG_ENUM         = 54, // A comment about A_LONG_ENUM
      AN_EVEN_LONGER_ENUM = 1,  // A comment about AN_EVEN_LONGER_ENUM
      
      +-----------------------------------------------+
      |  This is a table caption and it is very long  |
      +------------------+-----------------+----------+
      | AAAAAAAAA        |        B        |        C |
      +------------------+-----------------+----------+
      | 123              | foo             |          |
      | 000000005        | 6.283185        |          |
      |        a         | bbb             | c        |
      +------------------+-----------------+----------+
      
      |    AAAAAAAAA     |        B        |    C     |
      |:-----------------|:---------------:|---------:|
      | 123              | foo             |          |
      | 000000005        | 6.283185        |          |
      | a                | bbb             | c        |
      
Tags in Structs
Foo :: struct {
    a: [L]u8 `fmt:"s"`, // whole buffer is a string
    b: [N]u8 `fmt:"s,0"`, // 0 terminated string
    c: [M]u8 `fmt:"q,n"`, // string with length determined by n, and use %q rather than %s
    n: int `fmt:"-"`, // ignore this from formatting
}
Custom formatters
  • See fmt/example.odin .

Escaping symbols
  • %%

    • literal percent sign

  • {{

    • literal open brace

  • }}

    • literal close brace

Formatting Verbs
  • Using a verb in the wrong place does nothing, it just prints as if no formatting exists.

    • This is very strict. If it’s not in General, or the variable type, it won’t work.

  • General :

    • %v  / {:v}

      • The value in default format

      Tilesets: [Tileset{uid = 21, texture = Texture{id = 5, width = 384, height = 160, mipmaps = 1, format = "UNCOMPRESSED_R8G8B8A8"}, tilesize = [32, 32], pivot = [0, 0]}]
      
    • %w

      • An Odin-syntax representation of the value

      Tilesets: {Tileset{uid = 21, texture = Texture{id = 5, width = 384, height = 160, mipmaps = 1, format = PixelFormat.UNCOMPRESSED_R8G8B8A8}, tilesize = {32, 32}, pivot = {0, 0}}}
      
    • %T

      • An Odin-syntax representation of the type  of the value

      Tilesets: [dynamic]Tileset
      
    • %#v

      • An expanded format of %v with newlines and indentation

      Tilesets: [
          Tileset{
                  uid = 21,
                  texture = Texture{
                          id = 5,
                          width = 384,
                          height = 160,
                          mipmaps = 1,
                          format = "UNCOMPRESSED_R8G8B8A8",
                  },
                  tilesize = [
                          32,
                          32,
                  ],
                  pivot = [
                          0,
                          0,
                  ],
          },
      ]
      
  • Boolean :

    • %t

      • The word "true" or "false"

  • Integer :

    • %b

      • base 2

    • %c  / %r

      • the character represented by the corresponding Unicode code point

    • %o

      • base 8

      • Bytes.

    • %d  / %i

      • base 10

      • Decimal

      • Default for :

        • []byte

    • %z

      • base 12

    • %x

      • base 16, lower-case a-f

      • Hexadecimal

    • %X

      • base 16, upper-case A-F

      • Hexadecimal

    • %U

      • Unicode format: U+1234; same as "U+%04X"

  • Floating-point , complex numbers ,   quaternions :

    • %e

      • scientific notation, e.g. -1.23456e+78

    • %E

      • scientific notation, e.g. -1.23456E+78

    • %f  / %F

      • decimal point, no exponent, e.g. 123.456

    • %g  / %G

      • synonym for %f with default max precision

    • %h

      • hexadecimal (lower-case) with 0h prefix (0h01234abcd)

    • %H

      • hexadecimal (upper-case) with 0H prefix (0H01234ABCD)

    • %m

      • number of bytes in best unit, e.g. 123.45mib

    • %M

      • number of bytes in best unit, e.g. 123.45MiB

    • Width and Precision :

      • Width

        • optional decimal number after '%'.

        • Default: enough to represent value.

      • Precision

        • after width, period + decimal number.

        • No period: default precision.

        • Period alone: precision 0.

      • Measured in Unicode code points (runes).

      • n.b. C's printf uses bytes.

      • Examples :

        • %f

          • default width, default precision

        • %8f

          • width 8, default precision

        • %.2f

          • default width, precision 2

        • %8.3f

          • width 8, precision 3

        • %8.f

          • width 8, precision 0

  • String and slice of bytes :

    • %s

      • uninterpreted bytes of string/slice

    • %q

      • double-quoted string safely escaped with Odin syntax

    • %x

      • base 16, lower-case, two chars per byte

    • %X

      • base 16, upper-case, two chars per byte

  • Slice and dynamic array :

    • %p

      • address of 0th element in base 16 (upper-case), with 0x

  • Pointer :

    • %p

      • base 16, with 0x

    • %b , %d , %o , %z,   %x , %X

      • also work with pointers as integers

  • Enums :

    • %s

      • name of enum field

    • %i , %d , %f

      • also work as number

Flags
  • Ignored by verbs that don't expect them.

  • +

    • always print a sign for numeric values

  • -

    • pad spaces on right (left-justify)

  • #

    • Gives an alternative format.

    • %#b

      • add leading 0b for binary

    • %#o

      • add leading 0o for octal

    • %#z

      • add leading 0z for dozenal

    • %#x  / %#X

      • add leading 0x or 0X for hexadecimal

    • %#p

      • remove leading 0x for %p

    • %#m  / %#M

      • add a space between bytes and the unit of measurement

  • (space)

    • leave a space for elided sign in numbers (% d)

  • 0

    • Pad with leading zeros rather than spaces