Changeset 34120f10 in mainline for common/printf/printf_core.c


Ignore:
Timestamp:
2023-10-27T19:38:31Z (15 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, topic/msim-upgrade, topic/simplify-dev-export
Children:
63ed840
Parents:
c89ae25 (diff), 694ca3d6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge code deduplication work

TL;DR: Added directory /common, which now contains the sole existing
copy of ADT, printf_core, and a few other pieces. Should make changes
to any of those less of a headache.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • common/printf/printf_core.c

    rc89ae25 r34120f10  
    4040#include <stddef.h>
    4141#include <stdlib.h>
    42 #include <io/printf_core.h>
     42#include <printf_core.h>
    4343#include <ctype.h>
    4444#include <str.h>
    45 #include <double_to_str.h>
    46 #include <ieee_double.h>
    4745#include <assert.h>
    4846#include <macros.h>
    4947#include <uchar.h>
     48
     49/* Disable float support in kernel, because we usually disable floating operations there. */
     50#if __STDC_HOSTED__
     51#define HAS_FLOAT
     52#endif
     53
     54#ifdef HAS_FLOAT
     55#include <double_to_str.h>
     56#include <ieee_double.h>
     57#endif
    5058
    5159/** show prefixes 0x or 0 */
     
    122130static const char *digits_big = "0123456789ABCDEF";
    123131static const char invalch = U_SPECIAL;
    124 
    125 /** Unformatted double number string representation. */
    126 typedef struct {
    127         /** Buffer with len digits, no sign or leading zeros. */
    128         char *str;
    129         /** Number of digits in str. */
    130         int len;
    131         /** Decimal exponent, ie number = str * 10^dec_exp */
    132         int dec_exp;
    133         /** True if negative. */
    134         bool neg;
    135 } double_str_t;
    136 
    137 /** Returns the sign character or 0 if no sign should be printed. */
    138 static int get_sign_char(bool negative, uint32_t flags)
    139 {
    140         if (negative) {
    141                 return '-';
    142         } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
    143                 return '+';
    144         } else if (flags & __PRINTF_FLAG_SPACESIGN) {
    145                 return ' ';
    146         } else {
    147                 return 0;
    148         }
    149 }
    150 
    151 /** Prints count times character ch. */
    152 static int print_padding(char ch, int count, printf_spec_t *ps)
    153 {
    154         for (int i = 0; i < count; ++i) {
    155                 if (ps->str_write(&ch, 1, ps->data) < 0) {
    156                         return -1;
    157                 }
    158         }
    159 
    160         return count;
    161 }
    162132
    163133/** Print one or more characters without adding newline.
     
    585555}
    586556
     557#ifdef HAS_FLOAT
     558
     559/** Unformatted double number string representation. */
     560typedef struct {
     561        /** Buffer with len digits, no sign or leading zeros. */
     562        char *str;
     563        /** Number of digits in str. */
     564        int len;
     565        /** Decimal exponent, ie number = str * 10^dec_exp */
     566        int dec_exp;
     567        /** True if negative. */
     568        bool neg;
     569} double_str_t;
     570
     571/** Returns the sign character or 0 if no sign should be printed. */
     572static int get_sign_char(bool negative, uint32_t flags)
     573{
     574        if (negative) {
     575                return '-';
     576        } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
     577                return '+';
     578        } else if (flags & __PRINTF_FLAG_SPACESIGN) {
     579                return ' ';
     580        } else {
     581                return 0;
     582        }
     583}
     584
     585/** Prints count times character ch. */
     586static int print_padding(char ch, int count, printf_spec_t *ps)
     587{
     588        for (int i = 0; i < count; ++i) {
     589                if (ps->str_write(&ch, 1, ps->data) < 0) {
     590                        return -1;
     591                }
     592        }
     593
     594        return count;
     595}
     596
    587597/** Prints a special double (ie NaN, infinity) padded to width characters. */
    588598static int print_special(ieee_double_t val, int width, uint32_t flags,
     
    12291239        }
    12301240}
     1241
     1242#endif
    12311243
    12321244/** Print formatted string.
     
    15201532                                continue;
    15211533
     1534#ifdef HAS_FLOAT
    15221535                                /*
    15231536                                 * Floating point values
     
    15401553                                j = nxt;
    15411554                                continue;
     1555#endif
    15421556
    15431557                                /*
Note: See TracChangeset for help on using the changeset viewer.