|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Safe; |
| 4 | + |
| 5 | +use Safe\Exceptions\StringsException; |
| 6 | + |
| 7 | +/** |
| 8 | + * Returns a string produced according to the formatting string |
| 9 | + * format. |
| 10 | + * |
| 11 | + * @param string $format The format string is composed of zero or more directives: |
| 12 | + * ordinary characters (excluding %) that are |
| 13 | + * copied directly to the result and conversion |
| 14 | + * specifications, each of which results in fetching its |
| 15 | + * own parameter. |
| 16 | + * |
| 17 | + * A conversion specification follows this prototype: |
| 18 | + * %[argnum$][flags][width][.precision]specifier. |
| 19 | + * |
| 20 | + * An integer followed by a dollar sign $, |
| 21 | + * to specify which number argument to treat in the conversion. |
| 22 | + * |
| 23 | + * |
| 24 | + * Flags |
| 25 | + * |
| 26 | + * |
| 27 | + * |
| 28 | + * Flag |
| 29 | + * Description |
| 30 | + * |
| 31 | + * |
| 32 | + * |
| 33 | + * |
| 34 | + * - |
| 35 | + * |
| 36 | + * Left-justify within the given field width; |
| 37 | + * Right justification is the default |
| 38 | + * |
| 39 | + * |
| 40 | + * |
| 41 | + * + |
| 42 | + * |
| 43 | + * Prefix positive numbers with a plus sign |
| 44 | + * +; Default only negative |
| 45 | + * are prefixed with a negative sign. |
| 46 | + * |
| 47 | + * |
| 48 | + * |
| 49 | + * (space) |
| 50 | + * |
| 51 | + * Pads the result with spaces. |
| 52 | + * This is the default. |
| 53 | + * |
| 54 | + * |
| 55 | + * |
| 56 | + * 0 |
| 57 | + * |
| 58 | + * Only left-pads numbers with zeros. |
| 59 | + * With s specifiers this can |
| 60 | + * also right-pad with zeros. |
| 61 | + * |
| 62 | + * |
| 63 | + * |
| 64 | + * '(char) |
| 65 | + * |
| 66 | + * Pads the result with the character (char). |
| 67 | + * |
| 68 | + * |
| 69 | + * |
| 70 | + * |
| 71 | + * |
| 72 | + * |
| 73 | + * An integer that says how many characters (minimum) |
| 74 | + * this conversion should result in. |
| 75 | + * |
| 76 | + * A period . followed by an integer |
| 77 | + * who's meaning depends on the specifier: |
| 78 | + * |
| 79 | + * |
| 80 | + * |
| 81 | + * For e, E, |
| 82 | + * f and F |
| 83 | + * specifiers: this is the number of digits to be printed |
| 84 | + * after the decimal point (by default, this is 6). |
| 85 | + * |
| 86 | + * |
| 87 | + * |
| 88 | + * |
| 89 | + * For g and G |
| 90 | + * specifiers: this is the maximum number of significant |
| 91 | + * digits to be printed. |
| 92 | + * |
| 93 | + * |
| 94 | + * |
| 95 | + * |
| 96 | + * For s specifier: it acts as a cutoff point, |
| 97 | + * setting a maximum character limit to the string. |
| 98 | + * |
| 99 | + * |
| 100 | + * |
| 101 | + * |
| 102 | + * |
| 103 | + * If the period is specified without an explicit value for precision, |
| 104 | + * 0 is assumed. |
| 105 | + * |
| 106 | + * |
| 107 | + * |
| 108 | + * |
| 109 | + * Specifiers |
| 110 | + * |
| 111 | + * |
| 112 | + * |
| 113 | + * Specifier |
| 114 | + * Description |
| 115 | + * |
| 116 | + * |
| 117 | + * |
| 118 | + * |
| 119 | + * % |
| 120 | + * |
| 121 | + * A literal percent character. No argument is required. |
| 122 | + * |
| 123 | + * |
| 124 | + * |
| 125 | + * b |
| 126 | + * |
| 127 | + * The argument is treated as an integer and presented |
| 128 | + * as a binary number. |
| 129 | + * |
| 130 | + * |
| 131 | + * |
| 132 | + * c |
| 133 | + * |
| 134 | + * The argument is treated as an integer and presented |
| 135 | + * as the character with that ASCII. |
| 136 | + * |
| 137 | + * |
| 138 | + * |
| 139 | + * d |
| 140 | + * |
| 141 | + * The argument is treated as an integer and presented |
| 142 | + * as a (signed) decimal number. |
| 143 | + * |
| 144 | + * |
| 145 | + * |
| 146 | + * e |
| 147 | + * |
| 148 | + * The argument is treated as scientific notation (e.g. 1.2e+2). |
| 149 | + * The precision specifier stands for the number of digits after the |
| 150 | + * decimal point since PHP 5.2.1. In earlier versions, it was taken as |
| 151 | + * number of significant digits (one less). |
| 152 | + * |
| 153 | + * |
| 154 | + * |
| 155 | + * E |
| 156 | + * |
| 157 | + * Like the e specifier but uses |
| 158 | + * uppercase letter (e.g. 1.2E+2). |
| 159 | + * |
| 160 | + * |
| 161 | + * |
| 162 | + * f |
| 163 | + * |
| 164 | + * The argument is treated as a float and presented |
| 165 | + * as a floating-point number (locale aware). |
| 166 | + * |
| 167 | + * |
| 168 | + * |
| 169 | + * F |
| 170 | + * |
| 171 | + * The argument is treated as a float and presented |
| 172 | + * as a floating-point number (non-locale aware). |
| 173 | + * Available as of PHP 5.0.3. |
| 174 | + * |
| 175 | + * |
| 176 | + * |
| 177 | + * g |
| 178 | + * |
| 179 | + * |
| 180 | + * General format. |
| 181 | + * |
| 182 | + * |
| 183 | + * Let P equal the precision if nonzero, 6 if the precision is omitted, |
| 184 | + * or 1 if the precision is zero. |
| 185 | + * Then, if a conversion with style E would have an exponent of X: |
| 186 | + * |
| 187 | + * |
| 188 | + * If P > X ≥ −4, the conversion is with style f and precision P − (X + 1). |
| 189 | + * Otherwise, the conversion is with style e and precision P − 1. |
| 190 | + * |
| 191 | + * |
| 192 | + * |
| 193 | + * |
| 194 | + * G |
| 195 | + * |
| 196 | + * Like the g specifier but uses |
| 197 | + * E and f. |
| 198 | + * |
| 199 | + * |
| 200 | + * |
| 201 | + * o |
| 202 | + * |
| 203 | + * The argument is treated as an integer and presented |
| 204 | + * as an octal number. |
| 205 | + * |
| 206 | + * |
| 207 | + * |
| 208 | + * s |
| 209 | + * |
| 210 | + * The argument is treated and presented as a string. |
| 211 | + * |
| 212 | + * |
| 213 | + * |
| 214 | + * u |
| 215 | + * |
| 216 | + * The argument is treated as an integer and presented |
| 217 | + * as an unsigned decimal number. |
| 218 | + * |
| 219 | + * |
| 220 | + * |
| 221 | + * x |
| 222 | + * |
| 223 | + * The argument is treated as an integer and presented |
| 224 | + * as a hexadecimal number (with lowercase letters). |
| 225 | + * |
| 226 | + * |
| 227 | + * |
| 228 | + * X |
| 229 | + * |
| 230 | + * The argument is treated as an integer and presented |
| 231 | + * as a hexadecimal number (with uppercase letters). |
| 232 | + * |
| 233 | + * |
| 234 | + * |
| 235 | + * |
| 236 | + * |
| 237 | + * |
| 238 | + * General format. |
| 239 | + * |
| 240 | + * Let P equal the precision if nonzero, 6 if the precision is omitted, |
| 241 | + * or 1 if the precision is zero. |
| 242 | + * Then, if a conversion with style E would have an exponent of X: |
| 243 | + * |
| 244 | + * If P > X ≥ −4, the conversion is with style f and precision P − (X + 1). |
| 245 | + * Otherwise, the conversion is with style e and precision P − 1. |
| 246 | + * |
| 247 | + * The c type specifier ignores padding and width |
| 248 | + * |
| 249 | + * Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results |
| 250 | + * |
| 251 | + * Variables will be co-erced to a suitable type for the specifier: |
| 252 | + * |
| 253 | + * Type Handling |
| 254 | + * |
| 255 | + * |
| 256 | + * |
| 257 | + * Type |
| 258 | + * Specifiers |
| 259 | + * |
| 260 | + * |
| 261 | + * |
| 262 | + * |
| 263 | + * string |
| 264 | + * s |
| 265 | + * |
| 266 | + * |
| 267 | + * integer |
| 268 | + * |
| 269 | + * d, |
| 270 | + * u, |
| 271 | + * c, |
| 272 | + * o, |
| 273 | + * x, |
| 274 | + * X, |
| 275 | + * b |
| 276 | + * |
| 277 | + * |
| 278 | + * |
| 279 | + * double |
| 280 | + * |
| 281 | + * g, |
| 282 | + * G, |
| 283 | + * e, |
| 284 | + * E, |
| 285 | + * f, |
| 286 | + * F |
| 287 | + * |
| 288 | + * |
| 289 | + * |
| 290 | + * |
| 291 | + * |
| 292 | + * @param mixed $params |
| 293 | + * @return string Returns a string produced according to the formatting string |
| 294 | + * format. |
| 295 | + * @throws StringsException |
| 296 | + * |
| 297 | + */ |
| 298 | +function sprintf(string $format, ...$params): string |
| 299 | +{ |
| 300 | + if ($params !== []) { |
| 301 | + $result = \sprintf($format, ...$params); |
| 302 | + } else { |
| 303 | + $result = \sprintf($format); |
| 304 | + } |
| 305 | + return $result; |
| 306 | +} |
| 307 | + |
| 308 | +/** |
| 309 | + * Returns the portion of string specified by the |
| 310 | + * start and length parameters. |
| 311 | + * |
| 312 | + * @param string $string The input string. |
| 313 | + * @param int $start If start is non-negative, the returned string |
| 314 | + * will start at the start'th position in |
| 315 | + * string, counting from zero. For instance, |
| 316 | + * in the string 'abcdef', the character at |
| 317 | + * position 0 is 'a', the |
| 318 | + * character at position 2 is |
| 319 | + * 'c', and so forth. |
| 320 | + * |
| 321 | + * If start is negative, the returned string |
| 322 | + * will start at the start'th character |
| 323 | + * from the end of string. |
| 324 | + * |
| 325 | + * If string is less than |
| 326 | + * start characters long, FALSE will be returned. |
| 327 | + * |
| 328 | + * |
| 329 | + * Using a negative start |
| 330 | + * |
| 331 | + * |
| 332 | + * ]]> |
| 333 | + * |
| 334 | + * |
| 335 | + * @param int $length If length is given and is positive, the string |
| 336 | + * returned will contain at most length characters |
| 337 | + * beginning from start (depending on the length of |
| 338 | + * string). |
| 339 | + * |
| 340 | + * If length is given and is negative, then that many |
| 341 | + * characters will be omitted from the end of string |
| 342 | + * (after the start position has been calculated when a |
| 343 | + * start is negative). If |
| 344 | + * start denotes the position of this truncation or |
| 345 | + * beyond, FALSE will be returned. |
| 346 | + * |
| 347 | + * If length is given and is 0, |
| 348 | + * FALSE or NULL, an empty string will be returned. |
| 349 | + * |
| 350 | + * If length is omitted, the substring starting from |
| 351 | + * start until the end of the string will be |
| 352 | + * returned. |
| 353 | + * @return string Returns the extracted part of string;, or |
| 354 | + * an empty string. |
| 355 | + * @throws StringsException |
| 356 | + * |
| 357 | + */ |
| 358 | +function substr(string $string, int $start, int $length = null): string |
| 359 | +{ |
| 360 | + if ($length !== null) { |
| 361 | + $result = \substr($string, $start, $length); |
| 362 | + } else { |
| 363 | + $result = \substr($string, $start); |
| 364 | + } |
| 365 | + return $result; |
| 366 | +} |
0 commit comments