method
setbyte
ruby latest stable - Class:
String
setbyte(p1, p2)public
modifies the indexth byte as integer.
static VALUE
rb_str_setbyte(VALUE str, VALUE index, VALUE value)
{
long pos = NUM2LONG(index);
int byte = NUM2INT(value);
long len = RSTRING_LEN(str);
char *head, *ptr, *left = 0;
rb_encoding *enc;
int cr = ENC_CODERANGE_UNKNOWN, width, nlen;
if (pos < -len || len <= pos)
rb_raise(rb_eIndexError, "index %ld out of string", pos);
if (pos < 0)
pos += len;
if (!str_independent(str))
str_make_independent(str);
enc = STR_ENC_GET(str);
head = RSTRING_PTR(str);
ptr = &head[pos];
if (!STR_EMBED_P(str)) {
cr = ENC_CODERANGE(str);
switch (cr) {
case ENC_CODERANGE_7BIT:
left = ptr;
*ptr = byte;
if (ISASCII(byte)) goto end;
nlen = rb_enc_precise_mbclen(left, head+len, enc);
if (!MBCLEN_CHARFOUND_P(nlen))
ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
else
ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
goto end;
case ENC_CODERANGE_VALID:
left = rb_enc_left_char_head(head, ptr, head+len, enc);
width = rb_enc_precise_mbclen(left, head+len, enc);
*ptr = byte;
nlen = rb_enc_precise_mbclen(left, head+len, enc);
if (!MBCLEN_CHARFOUND_P(nlen))
ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
else if (MBCLEN_CHARFOUND_LEN(nlen) != width || ISASCII(byte))
ENC_CODERANGE_CLEAR(str);
goto end;
}
}
ENC_CODERANGE_CLEAR(str);
*ptr = byte;
end:
return value;
} Related methods
- Instance methods
- %
- *
- +
- +@
- -@
- <<
- <=>
- ==
- ===
- =~
- []
- []=
- ascii_only?
- b
- block_scanf
- bytes
- bytesize
- byteslice
- capitalize
- capitalize!
- casecmp
- casecmp?
- center
- chars
- chomp
- chomp!
- chop
- chop!
- chr
- clear
- codepoints
- concat
- count
- crypt
- delete
- delete!
- delete_prefix
- delete_prefix!
- delete_suffix
- delete_suffix!
- downcase
- downcase!
- dump
- each_byte
- each_char
- each_codepoint
- each_grapheme_cluster
- each_line
- empty?
- encode
- encode!
- encoding
- end_with?
- eql?
- force_encoding
- freeze
- getbyte
- grapheme_clusters
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- iseuc
- isjis
- issjis
- isutf8
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- match?
- next
- next!
- oct
- ord
- parse_csv
- partition
- prepend
- pretty_print
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- scrub
- scrub!
- setbyte
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ!
- sum
- swapcase
- swapcase!
- to_c
- to_d
- to_f
- to_i
- to_r
- to_s
- to_str
- to_sym
- toeuc
- tojis
- tolocale
- tosjis
- toutf16
- toutf32
- toutf8
- tr
- tr!
- tr_s
- tr_s!
- undump
- unicode_normalize
- unicode_normalize!
- unicode_normalized?
- unpack
- unpack1
- upcase
- upcase!
- upto
- valid_encoding?
- Class methods
- new
- try_convert