Commit Graph

510 Commits

Author SHA1 Message Date
KOBAYASHI Shuji d2f2f9db51 Remove location info from Exception#inspect
Because location info (file name and line number) is kept in the backtrace,
it should not be kept in the result of `inspect` (and the exception object
itself), I think.

### Example

  ```ruby
  # example.rb
  begin
    raise "err"
  rescue => e
    p e
  end
  ```

#### Before this patch:

  ```
  $ bin/mruby example.rb
  example.rb:2: err (RuntimeError)
  ```

#### After this patch:

  ```
  $ bin/mruby example.rb
  err (RuntimeError)
  ```
2019-12-14 22:29:37 +09:00
KOBAYASHI Shuji 7192429e83 Fix behavior of Kernel#Integer to numbers ending with _ and spaces
#### Before this patch:

  ```ruby
  Integer("1_ ")  #=> 1
  ```

#### After this patch (same as Ruby):

  ```ruby
  Integer("1_ ")  #=> ArgumentError
  ```
2019-12-11 16:40:39 +09:00
KOBAYASHI Shuji bf431e77b8 Fix behavior of String#to_i/Kernel#Integer to numbers starting with _
#### Before this patch:

  ```ruby
  Integer("_1")  #=> 1
  "_1".to_i      #=> 1
  ```

#### After this patch (same as Ruby):

  ```ruby
  Integer("_1")  #=> ArgumentError
  "_1".to_i      #=> 0
  ```
2019-12-10 21:31:30 +09:00
KOBAYASHI Shuji 0893ee492c Fix that String#to_f accepts consecutive _ as a numeric expression
Consecutive `_` is not allowed as a numeric expression:

  1_2__3             #=> SyntaxError
  Float("1_2__3")    #=> ArgumentError
  Integer("1_2__3")  #=> ArgumentError
  "1_2__3".to_i      #=> 12

But `String#to_f` accept it, so I fixed the issue.

Before this patch:

  "1_2__3".to_f      #=> 123

After this patch:

  "1_2__3".to_f      #=> 12
2019-12-09 21:47:59 +09:00
KOBAYASHI Shuji cf6ae0de6a Fix the error message of Kernel#Float
#### Before this patch:

  ```
  $ bin/mruby -e 'Float("1_a")'
  -e:1: invalid string for float(a) (ArgumentError)
  ```

#### After this patch:

  ```
  $ bin/mruby -e 'Float("1_a")'
  -e:1: invalid string for float("1_a") (ArgumentError)
  ```
2019-12-08 18:31:41 +09:00
KOBAYASHI Shuji 375c1ebc41 Rename BITSIZE to BIT and BIT to BIT_POS for consistency
The bit width terminology is unified to `BIT` according to `MRB_INT_BIT`
and `CHAR_BIT`. Also the bit position terminology is unified to `BIT_POS`.
2019-11-23 20:40:23 +09:00
KOBAYASHI Shuji a2df247063 Introduce mrb_ssize type for buffer size on memory; ref #4483
Previously, `mrb_int` was used as the type that represents the buffer size
on memory, but the sizes of `RString` and `RArray` exceed 6 words when
`MRB_INT64` is enabled on 32-bit CPU.

I don't think it is necessary to be able to represent the buffer size on
memory that exceeds the virtual address space. Therefore, for this purpose,
introduce `mrb_ssize` which doesn't exceed the sizes of `mrb_int` and
pointer.

I think all `mrb_int` used for this purpose should be changed to
`mrb_ssize`, but currently only the members of the structures (`RString`,
`mrb_shared_string`, `RArray` and `mrb_shared_array`) are changed.
2019-11-21 19:34:02 +09:00
KOBAYASHI Shuji 3241a7e5a8 Refactor mrb_string_value_cstr
- Keep `MRB_STR_ASCII` flag.
- Avoid a string object creation.
2019-11-19 01:07:58 +09:00
Yukihiro "Matz" Matsumoto e50344b5da Avoid unnecessary Symbol#to_s call; fix #4812 2019-11-08 23:38:40 +09:00
KOBAYASHI Shuji 45aa5081e4 Clear MRB_STR_SHARED flag in mrb_str_modify_keep_ascii; close #4807 2019-11-07 22:16:59 +09:00
KOBAYASHI Shuji bf0b591b2f Remove unused enum in mrb_cstr_to_dbl 2019-11-03 19:39:25 +09:00
KOBAYASHI Shuji 23cb78a0ff Remove documents about Regexp argument from String#{[],[]=} [ci skip] 2019-10-28 18:37:40 +09:00
KOBAYASHI Shuji 799ad0e1c7 Refine String#split document 2019-10-27 19:14:12 +09:00
KOBAYASHI Shuji db0a4d9045 Optimize chars2bytes with MRB_UTF8_STRING to ASCII only string
### Benchmark (with `MRB_UTF8_STRING`)

  ```
  $ mruby -e '
    COUNT = 150000
    SIZE = 10000
    strs = Array.new(COUNT) do
      s = "a" * SIZE
      s.size  # set `MRB_STR_ASCII` flag
      s
    end
    i = 0
    t = Time.now
    while i < COUNT
      strs[i][-2..-1] = ""
      i += 1
    end
    printf "%.2f sec\n", Time.now - t
  '

  1.10 sec  # before
  0.07 sec  # after
  ```
2019-10-26 22:19:04 +09:00
KOBAYASHI Shuji 06bd4ffdaf Optimize str_subseq with MRB_UTF8_STRING to ASCII only string
### Benchmark (with `MRB_UTF8_STRING`)

  ```ruby
  # benchmark.rb
  COUNT = 300000
  SIZE = 10000
  s = "a" * SIZE
  s.size  # set `MRB_STR_ASCII` flag
  i = 0
  while i < COUNT
    s[-1]
    i += 1
  end
  ```

#### Before this patch:

  ```
  $ time mruby benchmark.rb
          2.06 real         2.05 user         0.00 sys
  ```

#### After this patch:

  ```
  $ time mruby benchmark.rb
          0.05 real         0.04 user         0.00 sys
  ```
2019-10-23 19:03:39 +09:00
KOBAYASHI Shuji 41c43234a7 Fix incorrect MRB_STR_ASCII flag update in mrb_str_dump
### Example (with `MRB_UTF8_STRING`)

  ```ruby
  s = "\u3042"
  p s.size
  s.dump
  p s.size
  ```

#### Before this patch:

  ```
  1
  3
  ```

#### After this patch:

  ```
  1
  1
  ```
2019-10-22 21:56:40 +09:00
KOBAYASHI Shuji b0507e7678 Adjust buf size in str_escape 2019-10-15 19:23:30 +09:00
Yukihiro "Matz" Matsumoto d128e2d09e Merge pull request #4770 from shuujii/sHARED-string-is-not-required-when-sharing-POOL-string
SHARED string is not required when sharing POOL string
2019-10-12 23:35:43 +09:00
KOBAYASHI Shuji 7594983671 SHARED string is not required when sharing POOL string
The heap string buffer of POOL string always exists, does not need to be
released, and read only, so it can be shared as NOFREE string.
2019-10-12 21:32:56 +09:00
KOBAYASHI Shuji cdd36c979d Rename str_make_shared() to str_share() in src/string.c
Because it may not create `struct mrb_shared_string`.
2019-10-12 12:49:51 +09:00
KOBAYASHI Shuji 7ce5d33947 Integrate mrb_str_inspect and mrb_str_dump 2019-10-10 19:50:48 +09:00
Yukihiro "Matz" Matsumoto b7a8d538c1 Remove unnecessary function: mrb_str_freeze. 2019-10-06 22:18:11 +09:00
Yukihiro "Matz" Matsumoto 1f5a7f2f49 Freeze strings from nil.to_s, true.to_s, false.to_s.
This is an experimental changes in Ruby 2.7.
2019-10-04 16:02:50 +09:00
KOBAYASHI Shuji 198683e914 Simplify arguments check in String#rindex
Also fix document about type of the first argument.
2019-09-27 17:13:57 +09:00
KOBAYASHI Shuji feaf80d899 Use type predicate macros instead of mrb_type if possible
For efficiency with `MRB_WORD_BOXING` (implement type predicate macros for
all `enum mrb_vtype`).
2019-09-26 22:23:27 +09:00
Yukihiro "Matz" Matsumoto a365f9a67d Rename symbol-to-string functions; close #4684
* mrb_sym2name -> mrb_sym_name
* mrb_sym2name_len -> mrb_sym_name_len
* mrb_sym2str -> mrb_sym_str
2019-09-25 23:52:00 +09:00
KOBAYASHI Shuji e61095426b Simplify arguments check in String#index
Also fix document about type of the first argument.
2019-09-19 20:19:38 +09:00
Yukihiro "Matz" Matsumoto 57d7fe94a9 Add a macro mrb_frozen_p that points to MRB_FROZEN_P. 2019-09-14 23:21:44 +09:00
Yukihiro "Matz" Matsumoto fcd3f8450d Raise an error from String#<=> with a non string operand. 2019-09-14 09:05:32 +09:00
Yukihiro "Matz" Matsumoto 231a1d68b0 Remove $/ from mruby implementation.
1. `$/` and other Perl-ish global variables are not defined in ISO.
2. The current Ruby policy do not encourage those variables.
3. Those variables has global effect and can cause troubles.
2019-09-12 21:22:59 +09:00
Yukihiro "Matz" Matsumoto ec3aeede20 Move String#{getbyte,setbyte,byteslice} to the core; #4696
Unlike CRuby, there's no way to process strings byte-wise by core
methods because there's no per string encoding in mruby, so that
we moved 3 byte-wise operation methods from `mruby-string-ext` gem.
2019-09-11 18:47:39 +09:00
dearblue c9c27e9d69 Suppress warning by gcc with -Wmaybe-uninitialized 2019-08-24 11:44:51 +09:00
KOBAYASHI Shuji a84ab12735 Prioritize embedded string over nofree (or normal) string
Prioritize embedded string in the following functions:

- `str_new_static`
- `str_new`
- `mrb_str_new_capa`
- `mrb_str_pool`

The reasons are as follows:

- Consistency with `mrb_str_byte_subseq` and `str_replace`.
- Memory locality increases and may be slightly faster.
- No conversion cost to embedded string when modifying the string.
2019-08-21 12:06:03 +09:00
KOBAYASHI Shuji 8157672a29 Use RBasic padding for embedded string on 64-bit CPU
On 64-bit CPU, there is padding in `RBasic`, so reorder the fields and use
it as buffer of embedded string. This change allows 4 more bytes to be
embedded on 64-bit CPU.

However, an incompatibility will occur if `RString::as::ary` is accessed
directly because `RString` structure has changed.
2019-08-20 18:49:55 +09:00
KOBAYASHI Shuji 507dbf984e Move mrb_str_pool to src/string.c to use str_init family 2019-08-19 19:18:52 +09:00
Yukihiro "Matz" Matsumoto f263088b0d Merge pull request #4638 from dearblue/simplify-get-args
Simplify get arguments
2019-08-19 07:30:48 +09:00
Yukihiro "Matz" Matsumoto 1b9801a04b Merge pull request #4641 from shuujii/rename-mrb_shared_string-len-to-mrb_shared_string-capa
Rename `mrb_shared_string::len` to `mrb_shared_string::capa`
2019-08-19 07:24:06 +09:00
KOBAYASHI Shuji d2d201cc82 Rename mrb_shared_string::len to mrb_shared_string::capa
Because this field is used as capacity of string buffer.
2019-08-18 20:08:04 +09:00
KOBAYASHI Shuji aaa509dd07 Also use str_init_shared for orig in str_make_shared() 2019-08-18 19:44:00 +09:00
dearblue 05a873c25c Simplify get arguments
- `mrb_str_index_m()` and `mrb_str_rindex()`
  Make `mrb_get_args()` called only once from called twice.
- `mrb_str_byteslice()`
  Replace `goto` with `if ~ else`.
2019-08-18 15:38:25 +09:00
Yukihiro "Matz" Matsumoto 83dab1ee0d Merge pull request #4634 from shuujii/refactor-set-unset-string-type-flags
Refactor set/unset string type flags
2019-08-17 23:41:52 +09:00
KOBAYASHI Shuji caba5fef27 Refactor set/unset string type flags
Introduce `RSTR_SET_TYPE_FLAG` macro to set the specified string type flag and
clear the others.
2019-08-17 21:24:08 +09:00
dearblue bc27273087 Fix String#rindex with invalid UTF-8 string
Previously `String#rindex` returned the wrong index when given an
invalid UTF-8 string.

```terminal
% ruby26 -e 'str = "\xf0☀\xf1☁\xf2☂\xf3☃\xf0☀\xf1☁\xf2☂\xf3☃"; p str.rindex("☁")'
11
% ./mruby-head -e 'str = "\xf0☀\xf1☁\xf2☂\xf3☃\xf0☀\xf1☁\xf2☂\xf3☃"; p str.rindex("☁")'
nil
% ./mruby-patched -e 'str = "\xf0☀\xf1☁\xf2☂\xf3☃\xf0☀\xf1☁\xf2☂\xf3☃"; p str.rindex("☁")'
11
```
2019-08-17 12:05:03 +09:00
KOBAYASHI Shuji 9832e91304 SHARED/FSHARED string is not required when sharing NOFREE string
I think the string buffer of NOFREE string always exists and does not need
to be released, so it can be shared as another NOFREE string.

Also changed the `mrb_shared_string` field order so that eliminate padding if
`int` and `mrb_int` sizes are less than pointer size.
2019-08-16 22:05:58 +09:00
KOBAYASHI Shuji 59844ddde0 Remove unneeded #include in src/string.c 2019-08-15 12:18:33 +09:00
KOBAYASHI Shuji af6724c2f6 Extract initialization code of shared and fshared string to function 2019-08-14 22:20:34 +09:00
Yukihiro "Matz" Matsumoto 8ed15fd92e Small refactoring on #4630 2019-08-14 12:20:40 +09:00
KOBAYASHI Shuji 7af0ee099d Extract struct RString initialization code to function 2019-08-13 22:40:30 +09:00
Yukihiro "Matz" Matsumoto ad0b546088 Merge pull request #4626 from shuujii/mrb_str_modify_keep_ascii-can-embed-one-more-byte
`mrb_str_modify_keep_ascii` can embed one more byte
2019-08-12 17:09:21 +09:00
Yukihiro "Matz" Matsumoto 285ce9d8aa Replacing region may overwrap with the target region; fix #4627 2019-08-12 16:51:01 +09:00