Skip to content

Commit 7d8a4d4

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
Updated ti/sprintf docs
1 parent bc0ab26 commit 7d8a4d4

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

docs/headers/ti/sprintf.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
.. _sprintf_h:
22

33
ti/sprintf.h
4-
======================
4+
============
55

66
.. code-block:: c
77
88
#include <ti/sprintf.h>
99
10-
The OS comes with an implementation of ANSI C89 `sprintf`, which can reduce the size of a program by ~8 KiB. However, it is very limited in functionality. It does not support width specifiers, and it won't accept :code:`long` or :code:`float` arguments.
10+
The OS comes with an implementation of ANSI C89 `sprintf`, which can reduce the size of a program by ~8 KiB. However, it is very limited in functionality. It does not support length modifiers, and it won't accept :code:`long` or :code:`float` arguments.
1111

1212
boot_sprintf
13-
----------------------------
13+
------------
1414

15-
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`, alongside the flags :code:`-+#0*` in addition to the space flag and width field.
15+
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`, alongside the flags :code:`-+#0*` in addition to the space flag and minimum field width.
1616

1717
All length modifiers :code:`hh h l ll j z t L` and floating point specifiers :code:`%f %g %e %a` are **not** supported.
1818

1919
Additionally, each individual argument will write no more than 255 characters each. This means that any strings written with :code:`%s` will be truncated after 255 characters.
2020

21-
The :code:`<ti/sprintf.h>` provides `boot_sprintf`, in addition to `boot_snprintf` and `boot_asprintf` as macros.
21+
:code:`<ti/sprintf.h>` provides `boot_sprintf`, in addition to `boot_snprintf` and `boot_asprintf` as macros.
2222

2323
.. code-block:: c
2424
@@ -30,8 +30,8 @@ The :code:`<ti/sprintf.h>` provides `boot_sprintf`, in addition to `boot_snprint
3030
3131
Because the OS does not provide `vsprintf`, `boot_snprintf` and `boot_asprintf` are implemented as macros. This means that :code:`...` or :code:`__VA_ARGS__` will be evaluated twice when the macro is expanded. `sprintf` is traditionally "unsafe" because a maximum output length cannot be specified, which can cause buffer overflows. By writing to an unmapped memory address :code:`0xE40000`, `boot_sprintf` can safely write up to ~786000 bytes which can then be used to determine the length of the output.
3232

33-
replacing printf functions
34-
----------------------------
33+
Replacing printf functions
34+
--------------------------
3535

3636
To disable all other printf functions with `boot_sprintf`, `boot_snprintf`, and `boot_asprintf`, add the following line to the Makefile. More information :ref:`here <printf>`.
3737

@@ -44,12 +44,12 @@ To disable all other printf functions with `boot_sprintf`, `boot_snprintf`, and
4444
:code:`std::snprintf` and :code:`std::asprintf` will cause errors if :code:`HAS_PRINTF = NO`
4545

4646
boot_asprintf
47-
----------------------------
47+
-------------
4848

4949
`boot_asprintf` functions similarly to `asprintf <https://www.man7.org/linux/man-pages/man3/asprintf.3.html>`_. It will automatically allocate a buffer containing the output from `boot_sprintf`. The buffer shall be deallocated with :code:`free`. The returned buffer will be :code:`NULL` if an allocation or formatting error occurs.
5050

5151
boot_snprintf
52-
----------------------------
52+
-------------
5353

5454
`boot_snprintf` is similar to `snprintf`, except that it returns an empty string if the result of `boot_sprintf` won't fit inside the buffer. Otherwise passing in :code:`boot_snprintf(NULL, 0, format, args)` can be used to query the length of the output without writing to a buffer.
5555

@@ -58,7 +58,7 @@ The truncating behavior of C99 `snprintf` can be replicated with `boot_asprintf`
5858
.. code-block:: c
5959
6060
char buf[20];
61-
char* temp;
61+
char *temp;
6262
boot_asprintf(&temp, format, ...);
6363
if (temp != NULL) {
6464
strncpy(buf, temp, sizeof(buf));
@@ -67,12 +67,12 @@ The truncating behavior of C99 `snprintf` can be replicated with `boot_asprintf`
6767
}
6868
6969
printf and fprintf
70-
----------------------------
70+
------------------
7171
`printf` and `fprintf` can be replicated by using `boot_asprintf` and `fputs`
7272

7373
.. code-block:: c
7474
75-
char* output;
75+
char *output;
7676
boot_asprintf(&output, format, ...);
7777
if (output != NULL) {
7878
// fprintf(stdout, ...) == printf(...)

0 commit comments

Comments
 (0)