Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 1 | SYNOPSIS |
| 2 | string sprintf(string fmt, ...) |
| 3 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 4 | DESCRIPTION |
| 5 | Most of the characters in the format string (FMT) get passed |
| 6 | straight through to the output (ie: printed or put in the |
| 7 | return string), to format the arguments into the string it is |
| 8 | necessary to include an argument format string (AFS) in the |
| 9 | FMT. An AFS is a series of characters starting with a percent |
| 10 | sign "%" and terminated with a argument type specifier. |
| 11 | To include a "%" sign in the output, it is necessary to include a |
| 12 | double percent sign "%%". The sequence "%^" will output "%^" again. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 13 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 14 | Valid argument type specifiers are: |
| 15 | "s" : the argument is a string. |
| 16 | "d" : the argument is an integer to be included in decimal |
| 17 | representation. |
| 18 | "i" : same as "d". |
| 19 | "b" : the argument is an integer to be included in binary |
| 20 | representation. |
| 21 | "o" : the argument is an integer to be included in octal |
| 22 | representation. |
| 23 | "x" : the argument is an integer to be included in hexadecimal |
| 24 | representation. |
| 25 | "X" : as "x" except letters are capitalised. |
| 26 | "e","E","f","g","G" : the argument is a float to be included in |
| 27 | decimal representation; see examples for details |
| 28 | "c" : the argument is an int to included as a character |
| 29 | "O" : the argument is an LPC datatype to be printed in an arbituary |
| 30 | format, this is for debugging purposes. |
| 31 | If the argument is an object then the function |
| 32 | printf_obj_name() on the master object is called with the |
| 33 | object as a parameter, the string returned is included in |
| 34 | brackets at the end of object file name. |
| 35 | If 0 is returned then nothing is appended after the file name. |
| 36 | "Q" Like "O", except that special characters in strings are |
| 37 | printed in LPC notation. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 38 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 39 | Between the percent sign and the argument type specifier in |
| 40 | the AFS, the following modifiers can be included to specify |
| 41 | the formatting information. Order is not important unless |
| 42 | otherwise specified. "n" is used to specify a integer, which |
| 43 | can be a "*" in which case the next argument is used as the |
| 44 | number. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 45 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 46 | Modifiers: |
| 47 | n specifies the field size. If the size is prepended with |
| 48 | a 0, the argument is printed with leading zeroes. |
| 49 | "."n specifies the precision, for simple (not columns or tables) |
| 50 | strings specifies the truncation length. |
| 51 | ":"n n specifies both the field size _and_ the presision, if n is |
| 52 | prepended by a zero then the pad string is set to "0". |
| 53 | "'X'" the pad string is set to the char(s) between the single |
| 54 | quotes, if the field size is also prepended with a zero then |
| 55 | which ever is specified last will overrule. |
| 56 | NOTE: to include "'" in the pad string, you must use "\\'" |
| 57 | (as the backslash has to be escaped past the interpreter), |
| 58 | similarly, to include "\" requires "\\\\". |
| 59 | " " pad positive integers with a space. |
| 60 | "+" pad positive integers with a plus sign. |
| 61 | "-" left aligned within field size. |
| 62 | NB: std (s)printf() defaults to right alignment, which is |
| 63 | unnatural in the context of a mainly string based language |
| 64 | but has been retained for "compatibility" ;) |
| 65 | "|" centered within field size. |
| 66 | "$" justified to field size. Ignored unless the type specifier |
| 67 | is 's'. |
| 68 | "=" column mode. Ignored unless the argument type specifier is 's'. |
| 69 | Field size must be specified, if precision is specified then |
| 70 | it specifies the width for the string to be wordwrapped in, if |
| 71 | not then the field size is. The field size specifies the width |
| 72 | of the column and has the effect that the last line of the |
| 73 | column is padded with spaces to achieve this length. |
| 74 | "#" For strings: table mode. |
| 75 | Field size must be specified, if precision is |
| 76 | specified then it specifies the number of columns in |
| 77 | the table, otherwise the number is "optimally" |
| 78 | generated (as few lines and columns as possible). |
| 79 | Table mode is passed a list of backslash-n separated |
| 80 | 'words' which are put in a format similar to that of |
| 81 | ls. |
| 82 | For %O/%Q: compact output. |
| 83 | "@" the argument is an array. the corresponding AFS (minus all |
| 84 | "@") is applied to each element of the array. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 85 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 86 | When the formatting of an element results in several output lines |
| 87 | (column or table mode) and no explicit pad strings has been |
| 88 | defined, then the efun removes any padding whitespace before |
| 89 | the newlines of all but the last line. However, if an explicit |
| 90 | pad string has been given, even if it is the simple ' ', then |
| 91 | the padding will not be removed. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 92 | |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 93 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 94 | EXAMPLES |
| 95 | sprintf("decimal=%d, octal=%o, hexadecimal=%x\n", 7, 7, 7); |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 96 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 97 | sprintf("array=%O\n", ({1, 2, 3})); |
| 98 | this will return the following: |
| 99 | ({ /* sizeof() == 3 */ |
| 100 | 1, |
| 101 | 2, |
| 102 | 3 |
| 103 | }) |
| 104 | An array will be printed recursively and each element of the |
| 105 | array will be indented. Can also be used as a debugging tool. |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 106 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 107 | sprintf("%-*#s\n", 80, implode(get_dir("~/."), "\n")); |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 108 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 109 | sprintf("foo") // returns "foo" |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 110 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 111 | sprintf("%s","foo") // returns "foo" |
| 112 | sprintf("%7s","foo") // returns " foo" |
| 113 | sprintf("%-7s","foo") // returns "foo " |
| 114 | sprintf("%|7s","foo") // returns " foo " |
| 115 | sprintf("%7'.'s","foo") // returns "....foo" |
| 116 | sprintf("%-7'+-'s","foo") // returns "foo+-+-" |
| 117 | sprintf("%|9'-+'s","foo") // returns "-+-foo-+-" |
| 118 | sprintf("%3s","foobarbloh") // returns "foobarbloh" |
| 119 | sprintf("%3.6s","foobarbloh") // returns "foobar" |
| 120 | sprintf("%6.3s","foobarbloh") // returns " foo" |
| 121 | sprintf("%:6s","foobarbloh") // returns "foobar" |
| 122 | sprintf("%:3s","foobarbloh") // returns "foo" |
| 123 | sprintf("%*.*s",-7,2,"foobarbloh") // returns "fo " |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 124 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 125 | sprintf("%=12s","this is a very long sentence\n") |
| 126 | // returns " this is a\n" |
| 127 | // " very long\n" |
| 128 | // " sentence\n" |
| 129 | sprintf("%=-12s","this is a very long sentence\n") |
| 130 | // returns "this is a\n" |
| 131 | // "very long\n" |
| 132 | // "sentence\n" |
| 133 | sprintf("%=|12s","this is a very long sentence\n") |
| 134 | // returns " this is a\n" |
| 135 | // " very long\n" |
| 136 | // " sentence\n" |
| 137 | sprintf("%=10.6s","this is a very long sentence\n") |
| 138 | // returns " this\n" |
| 139 | // " is a\n" |
| 140 | // " very\n" |
| 141 | // " long\n" |
| 142 | // " senten\n" |
| 143 | // " ce\n" |
| 144 | sprintf("%#-40.3s\n", |
| 145 | "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n") |
| 146 | // returns "one five nine\n" |
| 147 | // "two six ten\n" |
| 148 | // "three seven \n" |
| 149 | // "four eight \n" |
| 150 | sprintf("%#-40s\n", |
| 151 | "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\n") |
| 152 | // returns "one three five seven nine\n" |
| 153 | // "two four six eight ten\n" |
| 154 | |
| 155 | sprintf("%@-5s",({"foo","bar","bloh"})) // returns "foo bar bloh " |
| 156 | |
| 157 | sprintf("%d",123) // returns "123" |
| 158 | sprintf("%7d",123) // returns " 123" |
| 159 | sprintf("%-7d",123) // returns "123 " |
| 160 | sprintf("%d/%d",123,-123) // returns "123/-123" |
| 161 | sprintf("% d/% d",123,-123) // returns " 123/-123" |
| 162 | sprintf("%+d/%+d",123,-123) // returns "+123/-123" |
| 163 | sprintf("%+5d/%5d",123,123) // returns " +123/ 123" |
| 164 | sprintf("%|6d",123) // returns " 123 " |
| 165 | sprintf("%|10d",123) // returns " 123 " |
| 166 | sprintf("%|10d%3s",123,"foo") // returns " 123 foo" |
| 167 | |
| 168 | sprintf("%o",16) // returns "20" |
| 169 | sprintf("%'0'3o",8) // returns "010" |
| 170 | |
| 171 | sprintf("%x",123) // returns "7b" |
| 172 | sprintf("%X",123) // returns "7B" |
| 173 | |
| 174 | sprintf("%f",123.5) // returns "124" |
| 175 | sprintf("%8.3f",123.5) // returns " 123.500" |
| 176 | sprintf("%E",123.5) // returns "1E+02" |
| 177 | sprintf("%12.4e",123.5) // returns " 1.2350e+02" |
| 178 | sprintf("%g",123.5) // returns "1e+02" |
| 179 | sprintf("%8.3G",123.5) // returns " 124" |
| 180 | sprintf("%8.6g",123.5) // returns " 123.5" |
| 181 | |
| 182 | HISTORY |
| 183 | LDMud 3.2.9 added the "%^" sequence for compatibility with |
| 184 | terminal_colour(), added the "%Q" sequence, clarified the meaning of |
| 185 | leading 0s in the field size modifier, clarified the interaction |
| 186 | between the padding and newlines, and added the '$' formatter for |
| 187 | justified printing of strings. |
| 188 | LDMud 3.2.10 added modifier '#' for '%O'/'%Q' and the datatype '%b'. |
| 189 | |
| 190 | SEE ALSO |
| 191 | printf(E), terminal_colour(E) |