doc.odin 5.3 KB

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