3

Is there any kind of narrow character function like there is iswctype() for wide characters?

UPDATE

I did not realize there was std::regex_traits::isctype(), otherwise I would have made the question explain the situation better:

I am actually implementing std::ctype<char> facet itself so I cannot use std::regex_traits::isctype() as itself depends on std::ctype<char>::is().

Please suggest POSIX or C99 function instead.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • If you're implementing `std::ctype`, how would C or POSIX functions help? – Cubbi Jan 07 '14 at 22:18
  • int isctype (int c) {return c >= 0 && (unsigned)(c+1) <= 256;} – Brandin Jan 07 '14 at 22:19
  • @Cubbi: The implementation can be limited to POSIX platforms. – wilx Jan 07 '14 at 22:20
  • @wilx Are you building a `std::ctype` around the POSIX `newlocale()` and want to support extended classifications such as (to use POSIX example) isvowel? – Cubbi Jan 07 '14 at 22:29
  • Perhaps you can explain the problem you are trying to solve, else this is an XY question. – Lightness Races in Orbit Jan 08 '14 at 02:07
  • @LightnessRacesinOrbit: I am implementing `std::ctype::is()`. How is that XY problem? – wilx Jan 08 '14 at 05:11
  • @wilx: Here is a handy link explaining what the XY problem is: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem. You're telling us what you're trying to do to attain some goal, and not what your goal is. – Lightness Races in Orbit Jan 08 '14 at 15:20
  • @wilx: Thank you for your well-informed and professional follow-up comment. It's always a pleasure. – Lightness Races in Orbit Jan 09 '14 at 02:08
  • Incidentally, how can your `ctype::is` classify a character against a locale-specific classification if it takes `ctype_base::mask` as an argument? There is no `std::ctype_base::vowel` or `std::ctype_base::kanji` – Cubbi Jan 13 '14 at 19:56

3 Answers3

2

There is a method provided by the Standard Regular Expression library named std::regex_traits::isctype. Note that it uses std::regex_traits which is a C++11 construct.

David G
  • 94,763
  • 41
  • 167
  • 253
2

POSIX says, for the extended character classes,

The charclass-name can be used as the property argument to the wctype() function, in regular expression and shell pattern-matching bracket expressions, and by the tr command.

and the POSIX regex page, correspondingly, says

In addition, character class expressions of the form:

[:name:]

are recognized in those locales where the name keyword has been given a charclass definition in the LC_CTYPE category.

So, to reach an extended classification in a narrow-character locale, the only POSIX-compliant way appears to be the POSIX regex class, much like the only way to get to it in C++ is the already-mentioned std::regex_traits::isctype.

Perhaps you're better off reaching into platform-specific APIs, in terms of which these functions are implemented (where accessible).

PS: Perhaps the more practical approach is to just call btowc and iswctype. A locale that classifies a narrow char and its widened form differently is questionable to say the least.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • "btowc and iswctype" -- I have thought about that before asking. I wanted to know whether there was something more direct. If this is the only way to do this, I guess I will use it then. – wilx Jan 08 '14 at 05:10
-3

They are functions declared in header <ctype.h> in C or <cctype> in C++ including isdigit, isalpha and so on.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335