| 1 |
#ifndef RBIMPL_ARITHMETIC_CHAR_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_ARITHMETIC_CHAR_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Arithmetic conversion between C's `char` and Ruby's. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/arithmetic/int.h" /* NUM2INT is here, but */ |
| 24 |
#include "ruby/internal/arithmetic/long.h" /* INT2FIX is here.*/ |
| 25 |
#include "ruby/internal/attr/artificial.h" |
| 26 |
#include "ruby/internal/attr/const.h" |
| 27 |
#include "ruby/internal/attr/constexpr.h" |
| 28 |
#include "ruby/internal/cast.h" |
| 29 |
#include "ruby/internal/core/rstring.h" |
| 30 |
#include "ruby/internal/value_type.h" |
| 31 |
|
| 32 |
#define RB_NUM2CHR rb_num2char_inline /**< @alias{rb_num2char_inline} */ |
| 33 |
#define NUM2CHR RB_NUM2CHR /**< @old{RB_NUM2CHR} */ |
| 34 |
#define CHR2FIX RB_CHR2FIX /**< @old{RB_CHR2FIX} */ |
| 35 |
|
| 36 |
/** @cond INTERNAL_MACRO */ |
| 37 |
#define RB_CHR2FIX RB_CHR2FIX |
| 38 |
/** @endcond */ |
| 39 |
|
| 40 |
RBIMPL_ATTR_CONST_UNLESS_DEBUG() |
| 41 |
RBIMPL_ATTR_CONSTEXPR_UNLESS_DEBUG(CXX14) |
| 42 |
RBIMPL_ATTR_ARTIFICIAL() |
| 43 |
/** |
| 44 |
* Converts a C's `unsigned char` into an instance of ::rb_cInteger. |
| 45 |
* |
| 46 |
* @param[in] c Arbitrary `unsigned char` value. |
| 47 |
* @return An instance of ::rb_cInteger. |
| 48 |
* |
| 49 |
* @internal |
| 50 |
* |
| 51 |
* Nobody explicitly states this but in Ruby, a char means an unsigned integer |
| 52 |
* value of range 0..255. This is a general principle. AFAIK there is no |
| 53 |
* single line of code where char is signed. |
| 54 |
*/ |
| 55 |
static inline VALUE |
| 56 |
RB_CHR2FIX(unsigned char c) |
| 57 |
{ |
| 58 |
return RB_INT2FIX(c); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Converts an instance of ::rb_cNumeric into C's `char`. At the same time it |
| 63 |
* accepts a String of more than one character, and returns its first byte. In |
| 64 |
* the early days there was a Ruby level "character" literal `?c`, which |
| 65 |
* roughly worked this way. |
| 66 |
* |
| 67 |
* @param[in] x Either a string or a numeric. |
| 68 |
* @exception rb_eTypeError `x` is not a numeric. |
| 69 |
* @exception rb_eRangeError `x` is out of range of `unsigned int`. |
| 70 |
* @return The passed value converted into C's `char`. |
| 71 |
*/ |
| 72 |
static inline char |
| 73 |
rb_num2char_inline(VALUE x) |
| 74 |
{ |
| 75 |
if (RB_TYPE_P(x, RUBY_T_STRING) && (RSTRING_LEN(x)>=1)) |
| 76 |
return RSTRING_PTR(x)[0]; |
| 77 |
else |
| 78 |
return RBIMPL_CAST((char)RB_NUM2INT(x)); |
| 79 |
} |
| 80 |
|
| 81 |
#endif /* RBIMPL_ARITHMETIC_CHAR_H */ |