How to change wchar.h so that wchar_t is the same type as wint_t?

wchar_t is defined inwchar.h

Currently, if developers want to use only wchar_t, they cannot do this without receiving type conversion warnings from the compiler. If wchar_tit has the same type as wint_t, it will be good for both sides. Developers who want to have both wint_t, and wchar_tin their own (for example, if they want their code to be compiled not only under glibc) can do this without receiving a compiler warning. Developers who want to use only wchar_t(to avoid unnecessary trouble with using wint_tand explicit type conversion) can also do this without receiving compiler warnings. And it will not bring any problems with incompatibility or portability, except that if the code using onlywchar_twill be compiled on a machine that uses the original one wchar.h, the compiler will print these suspicious warnings (if -Wconversionenabled), but the compiled program will work exactly the same.

C standard (9899: 201x 7.29) mentions:

wchar_t and wint_t can be the same integer types.

In addition, in glibc, wide characters are always ISO10646 / Unicode / UCS-4, so they always use 4 bytes. Thus, nothing prevents the creation of wchar_tthe same type as wint_tin glibc.

But it seems that the developers glibc does not want to do wint_t, and wchar_tthe same type, for whatever reason. So I want to change the local copy wchar.h.

ISO10646 / Unicode / UCS-4 uses values 2^31for the extended character set (MSB is not used):

0xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

, 4- 2^31 (MSB - "1" ):

1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

WEOF, , 4- WEOF.

, glibc wchar.h, wint_t ( -1 0xffffffff MSB "1", , MSB ISO10646/Unicode/UCS-4).

wchar_t - wchar.h. , wchar_t , wint_t?

#ifndef _WCHAR_H

#if !defined __need_mbstate_t && !defined __need_wint_t
# define _WCHAR_H 1
# include <features.h>
#endif

#ifdef _WCHAR_H
/* Get FILE definition.  */
# define __need___FILE
# if defined __USE_UNIX98 || defined __USE_XOPEN2K
#  define __need_FILE
# endif
# include <stdio.h>
/* Get va_list definition.  */
# define __need___va_list
# include <stdarg.h>

# include <bits/wchar.h>

/* Get size_t, wchar_t, wint_t and NULL from <stddef.h>.  */
# define __need_size_t
# define __need_wchar_t
# define __need_NULL
#endif
#if defined _WCHAR_H || defined __need_wint_t || !defined __WINT_TYPE__
# undef __need_wint_t
# define __need_wint_t
# include <stddef.h>

/* We try to get wint_t from <stddef.h>, but not all GCC versions define it
   there.  So define it ourselves if it remains undefined.  */
# ifndef _WINT_T
/* Integral type unchanged by default argument promotions that can
   hold any value corresponding to members of the extended character
   set, as well as at least one value that does not correspond to any
   member of the extended character set.  */
#  define _WINT_T
typedef unsigned int wint_t;
# else
/* Work around problems with the <stddef.h> file which doesn't put
   wint_t in the std namespace.  */
#  if defined __cplusplus && defined _GLIBCPP_USE_NAMESPACES \
      && defined __WINT_TYPE__
__BEGIN_NAMESPACE_STD
typedef __WINT_TYPE__ wint_t;
__END_NAMESPACE_STD
#  endif
# endif

/* Tell the caller that we provide correct C++ prototypes.  */
# if defined __cplusplus && __GNUC_PREREQ (4, 4)
#  define __CORRECT_ISO_CPP_WCHAR_H_PROTO
# endif
#endif

#if (defined _WCHAR_H || defined __need_mbstate_t) && !defined ____mbstate_t_defined
# define ____mbstate_t_defined  1
/* Conversion state information.  */
typedef struct
{
  int __count;
  union
  {
# ifdef __WINT_TYPE__
    __WINT_TYPE__ __wch;
# else
    wint_t __wch;
# endif
    char __wchb[4];
  } __value;        /* Value so far.  */
} __mbstate_t;
#endif
#undef __need_mbstate_t


/* The rest of the file is only used if used if __need_mbstate_t is not
   defined.  */
#ifdef _WCHAR_H

# ifndef __mbstate_t_defined
__BEGIN_NAMESPACE_C99
/* Public type.  */
typedef __mbstate_t mbstate_t;
__END_NAMESPACE_C99
#  define __mbstate_t_defined 1
# endif

#ifdef __USE_GNU
__USING_NAMESPACE_C99(mbstate_t)
#endif

#ifndef WCHAR_MIN
/* These constants might also be defined in <inttypes.h>.  */
# define WCHAR_MIN __WCHAR_MIN
# define WCHAR_MAX __WCHAR_MAX
#endif

#ifndef WEOF
# define WEOF (0xffffffffu)
#endif

/* For XPG4 compliance we have to define the stuff from <wctype.h> here
   as well.  */
#if defined __USE_XOPEN && !defined __USE_UNIX98
# include <wctype.h>
#endif


__BEGIN_DECLS

__BEGIN_NAMESPACE_STD
/* This incomplete type is defined in <time.h> but needed here because
   of `wcsftime'.  */
struct tm;
__END_NAMESPACE_STD
/* XXX We have to clean this up at some point.  Since tm is in the std
   namespace but wcsftime is in __c99 the type wouldn't be found
   without inserting it in the global namespace.  */
__USING_NAMESPACE_STD(tm)
0
2

, wint_t , wchar_t , " " printf() et al. , , printf():

wchar_t wc = …;
printf("%lc", wc);

wc wint_t. printf(), va_arg() <stdarg.h>, wint_t .

, wint_t , wchar_t, wchar_t (16-) short ( unsigned short), wint_t (32- ) int. wint_t , wchar_t 16- . , . , int 16- , .

ISO/IEC 9899: 2011

7.29 <wchar.h>

7.29.1

¶1 <wchar.h> , . 326)

2 wchar_t size_t ( 7.19);

mbstate_t

, , , ;

wint_t

, , , , , , (. WEOF), 327)...

326) . " (7.31.16).
327)wchar_t wint_t .

§7.19 <stddef.h>

¶2...

wchar_t

, , ; . , , __STDC_MB_MIGHT_NEQ_WC__.

. putchar(), fputc() putc() char , C. , , .

+1

-Wconversion compiler, wint_t wchar_t '#define WEOF (-1)' wchar.h wctype.h

wchar.h :

sudo perl -i -pe 'print qq(#define WEOF (-1)\n) if $.==1; next unless /Copy SRC to DEST\./..eof; s/\bwint_t\b/wchar_t/g' /usr/include/wchar.h

wctype.h :

sudo perl -i -pe 'print qq(#define WEOF (-1)\n) if $.==1; next unless /Wide-character classification functions/..eof; s/\bwint_t\b/wchar_t/g' /usr/include/wctype.h

, , wint_t, wint_t wchar_t .

.

Unix- wchar_t 16- Unicode. , , Unicode ISO 10646 UTF-16, . wchar_t.

, , UTF-8, , , 31:

1111110x    10xxxxxx    10xxxxxx    10xxxxxx    10xxxxxx    10xxxxxx

, , wint_t ( Unicode 4- (.. 32-) ). , " " - , . , ( UTF-8 ).

, - wint_t . , . man mbstowcs. wchar_t iswlower() wctype.h, wint_t.

0

Source: https://habr.com/ru/post/1661439/


All Articles