doc.odin 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. package fmt implements formatted I/O with procedures similar to C's printf and Python's format.
  3. The format 'verbs' are derived from C's but simpler.
  4. Printing
  5. The verbs:
  6. General:
  7. %v the value in a default format
  8. %#v an expanded format of %v with newlines and indentation
  9. %T an Odin-syntax representation of the type of the value
  10. %% a literal percent sign; consumes no value
  11. {{ a literal open brace; consumes no value
  12. }} a literal close brace; consumes no value
  13. {:v} equivalent to %v (Python-like formatting syntax)
  14. Boolean:
  15. %t the word "true" or "false"
  16. Integer:
  17. %b base 2
  18. %c the character represented by the corresponding Unicode code point
  19. %r synonym for %c
  20. %o base 8
  21. %d base 10
  22. %i base 10
  23. %z base 12
  24. %x base 16, with lower-case letters for a-f
  25. %X base 16, with upper-case letters for A-F
  26. %U Unicode format: U+1234; same as "U+%04X"
  27. Floating-point, complex numbers, and quaternions:
  28. %e scientific notation, e.g. -1.23456e+78
  29. %E scientific notation, e.g. -1.23456E+78
  30. %f decimal point but no exponent, e.g. 123.456
  31. %F synonym for %f
  32. %g synonym for %f with default maximum precision
  33. %G synonym for %g
  34. %h hexadecimal (lower-case) representation with 0h prefix (0h01234abcd)
  35. %H hexadecimal (upper-case) representation with 0H prefix (0h01234ABCD)
  36. %m number of bytes in the best unit of measurement, e.g. 123.45mib
  37. %M number of bytes in the best unit of measurement, e.g. 123.45MiB
  38. String and slice of bytes
  39. %s the uninterpreted bytes of the string or slice
  40. %q a double-quoted string safely escaped with Odin syntax
  41. %x base 16, lower-case, two characters per byte
  42. %X base 16, upper-case, two characters per byte
  43. Slice and dynamic array:
  44. %p address of the 0th element in base 16 notation (upper-case), with leading 0x
  45. Pointer:
  46. %p base 16 notation (upper-case), with leading 0x
  47. The %b, %d, %o, %z, %x, %X verbs also work with pointers,
  48. treating it as if it was an integer
  49. Enums:
  50. %s prints the name of the enum field
  51. The %i, %d, %f verbs also work with enums,
  52. treating it as if it was a number
  53. For compound values, the elements are printed using these rules recursively; laid out like the following:
  54. struct: {name0 = field0, name1 = field1, ...}
  55. array [elem0, elem1, elem2, ...]
  56. enumerated array [key0 = elem0, key1 = elem1, key2 = elem2, ...]
  57. maps: map[key0 = value0, key1 = value1, ...]
  58. bit sets {key0 = elem0, key1 = elem1, ...}
  59. pointer to above: &{}, &[], &map[]
  60. Width is specified by an optional decimal number immediately after the '%'.
  61. If not present, the width is whatever is necessary to represent the value.
  62. Precision is specified after the (optional) width by a period followed by a decimal number.
  63. If no period is present, a default precision is used.
  64. A period with no following number specifies a precision of 0.
  65. Examples:
  66. %f default width, default precision
  67. %8f width 8, default precision
  68. %.2f default width, precision 2
  69. %8.3f width 8, precision 3
  70. %8.f width 8, precision 0
  71. Width and precision are measured in units of Unicode code points (runes).
  72. n.b. C's printf uses units of bytes.
  73. Other flags:
  74. + always print a sign for numeric values
  75. - pad with spaces on the right rather the left (left-justify the field)
  76. # alternate format:
  77. add leading 0b for binary (%#b)
  78. add leading 0o for octal (%#o)
  79. add leading 0z for dozenal (%#z)
  80. add leading 0x or 0X for hexadecimal (%#x or %#X)
  81. remove leading 0x for %p (%#p)
  82. add a space between bytes and the unit of measurement (%#m or %#M)
  83. ' ' (space) leave a space for elided sign in numbers (% d)
  84. 0 pad with leading zeros rather than spaces
  85. Flags are ignored by verbs that don't expect them.
  86. For each printf-like procedure, there is a print function that takes no
  87. format, and is equivalent to doing %v for every value and inserts a separator
  88. between each value (default is a single space).
  89. Another procedure println which has the same functionality as print but appends a newline.
  90. Explicit argument indices:
  91. In printf-like procedures, the default behaviour is for each formatting verb to format successive
  92. arguments passed in the call. However, the notation [n] immediately before the verb indicates that
  93. the nth zero-index argument is to be formatted instead.
  94. The same notation before an '*' for a width or precision specifier selects the argument index
  95. holding the value.
  96. Python-like syntax with argument indices differs for selecting the argument index: {n:v}
  97. Examples:
  98. fmt.printfln("%[1]d %[0]d", 13, 37) // C-like syntax
  99. fmt.printfln("{1:d} {0:d}", 13, 37) // Python-like syntax
  100. prints "37 13", whilst:
  101. fmt.printfln("%*[2].*[1][0]f", 17.0, 2, 6) // C-like syntax
  102. fmt.printfln("{0:*[2].*[1]f}", 17.0, 2, 6) // Python-like syntax
  103. is equivalent to:
  104. fmt.printfln("%6.2f", 17.0) // C-like syntax
  105. fmt.printfln("{:6.2f}", 17.0) // Python-like syntax
  106. and prints "17.00".
  107. Format errors:
  108. If an invalid argument is given for a verb, such as providing a string to %d, the generated string
  109. will contain a description of the problem. For example:
  110. Bad enum value:
  111. %!(BAD ENUM VALUE)
  112. Too many arguments:
  113. %!(EXTRA <value>, <value>, ...)
  114. Too few arguments:
  115. %!(MISSING ARGUMENT)
  116. Invalid width or precision
  117. %!(BAD WIDTH)
  118. %!(BAD PRECISION)
  119. Missing verb:
  120. %!(NO VERB)
  121. Invalid or invalid use of argument index:
  122. %!(BAD ARGUMENT NUMBER)
  123. Missing close brace when using Python-like formatting syntax:
  124. %!(MISSING CLOSE BRACE)
  125. */
  126. package fmt