doc.odin 5.6 KB

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