doc.odin 5.5 KB

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