C function - isgraph ()

Does anyone know how the isgraph() function works in C? I understand its use and results, but the code behind it is what interests me.

For example, it only looks at the char value and compares it with the ASCII table? Or does he really check if it can be displayed? If so, how?

+4
source share
4 answers

The code of the isgraph() function depends on the platform (or, more precisely, on the implementation). One common way is to use an initialized array of bit fields, one per character in the (single-byte) set of codes plus EOF (which must be accepted by the functions), and then select the corresponding bit. This allows a simple implementation as a macro that is safe (evaluates its argument only once) and as a simple (possibly built-in) function.

 #define isgraph(x) (__charmap[(x)+1]&__PRINT) 

where __charmap and __PRINT are names reserved for implementation. Part +1 deals with the general situation where EOF is -1 .


In accordance with standard C (ISO / IEC 9899: 1999):

§7.4.1.6. Graphic function

Summary

 #include <ctype.h> int isgraph(int c); 

Description

The check function is performed for any print character except a space ('').

and

§7.4 Character Handling <ctype.h>

¶1 The heading declares several functions useful for classifying and matching characters. 166) In all cases, the argument is an int whose value must be represented as an unsigned char or equal to the value of the EOF macro. If the argument has any other value, the behavior is undefined.

¶2 The behavior of these functions is affected by the current locale. Those functions that have locale-specific aspects only when not in the "C" locale are noted below.

¶3 The term "printable character" refers to a member of a locale-specific character set, each of which occupies a single printing position on a display device; the term control character refers to a member of a locale-specific character set that does not print characters. 167) All letters and numbers print characters.

166) See “Future Directions of the Library (7.26.2).

167) In an implementation using a seven-bit US ASCII character set, these print characters whose values ​​range from 0x20 (space) to 0x7E (tilde); control characters are those whose values ​​range from 0 (NUL) to 0x1F (US) and the character 0x7F (DEL).

+6
source

It is called isgraph, not isGraph (and char, not Char), and the POSIX Programmer manual says

The isgraph () function must check whether the graph in the current language program of the program is a class symbol; see Basic Definitions of IEEE Std 1003.1-2001, Chapter 7, Language.

So yes, it looks in a table (or equivalent code). It cannot check if it can actually be displayed, since it depends on the output device, many of which can display characters in addition to those for which isgraph returns true.

+2
source

The isgraph () macro only considers an ASCII table or version of your location / country / providence / planet / galaxy ASCII table.

Here's the test code for Counting Words , in which you can increase performance by writing your own version, which initializes the bool [256] array using isgraph (). There are control results with the code.

Since the bool / arrays variables are actually BYTE and not bits, you can do even better in terms of memory efficiency if you use a bit array and check this out. It happily takes only 32 bytes. It will almost certainly receive money on any modern general-purpose processor.

It is important to note that if you want to slightly differ from the standard ones presented here (see the graphic image of character tests) , you are free to change the initialization provided by the standard test to include your own exceptions.

Truth table for various character test macros

+1
source

isgraph checks for "printable" characters, but the definition of "printable" may vary depending on your language. Your region may use characters that are not in an ASCII table. Inside, it most likely is a table search, a test based on the range ( (x >= 'a') && (x <= 'z') , etc.) Or a combination of them. Different implementations can do this a little differently.

0
source

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


All Articles