Class: OverSIP::WebSocket::FramingUtils::Utf8Validator

Inherits:
Object
  • Object
show all
Defined in:
ext/websocket_framing_utils/ws_framing_utils_ruby.c

Instance Method Summary collapse

Instance Method Details

#resetObject



78
79
80
81
82
83
84
85
86
87
# File 'ext/websocket_framing_utils/ws_framing_utils_ruby.c', line 78

VALUE Utf8Validator_reset(VALUE self)
{
  TRACE();
  utf8_validator *validator = NULL;
  DATA_GET(self, utf8_validator, validator);

  validator->state = UTF8_ACCEPT;

  return Qnil;
}

#validate(string) ⇒ Object

Returns:

  • true: Valid UTF-8 string.

  • nil: Valid but not terminated UTF-8 string.

  • false: Invalid UTF-8 string.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/websocket_framing_utils/ws_framing_utils_ruby.c', line 96

VALUE Utf8Validator_validate(VALUE self, VALUE string)
{
  TRACE();
  utf8_validator *validator = NULL;
  uint8_t *str = NULL;
  int i;

  REQUIRE_TYPE(string, T_STRING);
  str = RSTRING_PTR(string);

  DATA_GET(self, utf8_validator, validator);

  for(i=0; i < RSTRING_LEN(string); i++)
    if (utf8_decode(&validator->state, &validator->codepoint, str[i]) == UTF8_REJECT)
      return Qfalse;

  switch(validator->state) {
    case UTF8_ACCEPT:
      return Qtrue;
      break;
    default:
      return Qnil;
      break;
  }
}