<commit_before># encoding: utf-8
require_relative 'spec_helper'
require_relative '../../shared/string/times'

load_extension('string')

class CApiStringSpecs
  class ValidTostrTest
    def to_str
      "ruby"
    end
  end

  class InvalidTostrTest
    def to_str
      []
    end
  end

  class ToSOrInspect
    def to_s
      'A string'
    end

    def inspect
      'A different string'
    end
  end
end

describe :rb_str_new2, shared: true do
  it "returns a new string object calling strlen on the passed C string" do
    # Hardcoded to pass const char * = "hello\0invisible"
    @s.send(@method, "hello\0not used").should == "hello"
  end

  it "encodes the string with ASCII_8BIT" do
    @s.send(@method, "hello").encoding.should == Encoding::ASCII_8BIT
  end
end

describe "C-API String function" do
  before :each do
    @s = CApiStringSpecs.new
  end

  [Encoding::BINARY, Encoding::UTF_8].each do |enc|
    describe "rb_str_set_len on a #{enc.name} String" do
      before :each do
        @str = "abcdefghij".force_encoding(enc)
        # Make sure to unshare the string
        @s.rb_str_modify(@str)
      end

      it "reduces the size of the string" do
        @s.rb_str_set_len(@str, 5).should == "abcde"
      end

      it "inserts a NULL byte at the length" do
        @s.rb_str_set_len(@str, 5).should == "abcde"
        @s.rb_str_set_len(@str, 8).should == "abcde\x00gh"
      end

      it "updates the byte size" do
        @s.rb_str_set_len(@str, 4)
        @str.bytesize.should == 4
        @str.should == "abcd"
      end

      it "invalidates the character size" do
        @str.size.should == 10
        @s.rb_str_set_len(@str, 4)
        @str.size.should == 4
        @str.should == "abcd"
      end

      it "invalidates the code range" do
        @s.rb_str_set_len(@str, 4)
        @str.ascii_only?.should == true
      end

      it "updates the string's attributes visible in C code" do
        @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4
      end

      it "can reveal characters written from C with RSTRING_PTR" do
        @s.rb_str_set_len(@str, 1)
        @str.should == "a"

        @s.RSTRING_PTR_set(@str, 1, 'B'.ord)
        @s.RSTRING_PTR_set(@str, 2, 'C'.ord)
        @s.rb_str_set_len(@str, 3)

        @str.bytesize.should == 3
        @str.should == "aBC"
      end
    end
  end

  describe "rb_str_buf_new" do
    it "returns the equivalent of an empty string" do
      buf = @s.rb_str_buf_new(10, nil)
      buf.should == ""
      buf.bytesize.should == 0
      buf.size.should == 0
      @s.RSTRING_LEN(buf).should == 0
    end

    it "returns a string with the given capacity" do
      buf = @s.rb_str_buf_new(256, nil)
      @s.rb_str_capacity(buf).should == 256
    end

    it "returns a string that can be appended to" do
      str = @s.rb_str_buf_new(10, "defg")
      str << "abcde"
      str.should == "abcde"
    end

    it "returns a string that can be concatenated to another string" do
      str = @s.rb_str_buf_new(10, "defg")
      ("abcde" + str).should == "abcde"
    end

    it "returns a string whose bytes can be accessed by RSTRING_PTR" do
      str = @s.rb_str_buf_new(10, "abcdefghi")
      @s.rb_str_new(str, 10).should == "abcdefghi\x00"
    end

    it "returns a string that can be modified by rb_str_set_len" do
      str = @s.rb_str_buf_new(10, "abcdef")
      @s.rb_str_set_len(str, 4)
      str.should == "abcd"

      @s.rb_str_set_len(str, 8)
      str[0, 6].should == "abcd\x00f"
      @s.RSTRING_LEN(str).should == 8
    end

    it "can be used as a general buffer and reveal characters with rb_str_set_len" do
      str = @s.rb_str_buf_new(10, "abcdef")

      @s.RSTRING_PTR_set(str, 0, 195)
      @s.RSTRING_PTR_set(str, 1, 169)
      @s.rb_str_set_len(str, 2)

      str.force_encoding(Encoding::UTF_8)
      str.bytesize.should == 2
      str.size.should == 1
      str.should == "é"
    end
  end

  describe "rb_str_buf_new2" do
    it "returns a new string object calling strlen on the passed C string" do
      # Hardcoded to pass const char * = "hello\0invisible"
      @s.rb_str_buf_new2.should == "hello"
    end
  end

  describe "rb_str_new" do
    it "creates a new String with ASCII-8BIT Encoding" do
      @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT
    end

    it "returns a new string object from a char buffer of len characters" do
      @s.rb_str_new("hello", 3).should == "hel"
    end

    it "returns an empty string if len is 0" do
      @s.rb_str_new("hello", 0).should == ""
    end

    it "copy length bytes and does not stop at the first \\0 byte" do
      @s.rb_str_new("he\x00llo", 6).should == "he\x00llo"
      @s.rb_str_new_native("he\x00llo", 6).should == "he\x00llo"<commit_msg>rb_str_new() Strings should not be tainted<commit_after># encoding: utf-8
require_relative 'spec_helper'
require_relative '../../shared/string/times'

load_extension('string')

class CApiStringSpecs
  class ValidTostrTest
    def to_str
      "ruby"
    end
  end

  class InvalidTostrTest
    def to_str
      []
    end
  end

  class ToSOrInspect
    def to_s
      'A string'
    end

    def inspect
      'A different string'
    end
  end
end

describe :rb_str_new2, shared: true do
  it "returns a new string object calling strlen on the passed C string" do
    # Hardcoded to pass const char * = "hello\0invisible"
    @s.send(@method, "hello\0not used").should == "hello"
  end

  it "encodes the string with ASCII_8BIT" do
    @s.send(@method, "hello").encoding.should == Encoding::ASCII_8BIT
  end
end

describe "C-API String function" do
  before :each do
    @s = CApiStringSpecs.new
  end

  [Encoding::BINARY, Encoding::UTF_8].each do |enc|
    describe "rb_str_set_len on a #{enc.name} String" do
      before :each do
        @str = "abcdefghij".force_encoding(enc)
        # Make sure to unshare the string
        @s.rb_str_modify(@str)
      end

      it "reduces the size of the string" do
        @s.rb_str_set_len(@str, 5).should == "abcde"
      end

      it "inserts a NULL byte at the length" do
        @s.rb_str_set_len(@str, 5).should == "abcde"
        @s.rb_str_set_len(@str, 8).should == "abcde\x00gh"
      end

      it "updates the byte size" do
        @s.rb_str_set_len(@str, 4)
        @str.bytesize.should == 4
        @str.should == "abcd"
      end

      it "invalidates the character size" do
        @str.size.should == 10
        @s.rb_str_set_len(@str, 4)
        @str.size.should == 4
        @str.should == "abcd"
      end

      it "invalidates the code range" do
        @s.rb_str_set_len(@str, 4)
        @str.ascii_only?.should == true
      end

      it "updates the string's attributes visible in C code" do
        @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4
      end

      it "can reveal characters written from C with RSTRING_PTR" do
        @s.rb_str_set_len(@str, 1)
        @str.should == "a"

        @s.RSTRING_PTR_set(@str, 1, 'B'.ord)
        @s.RSTRING_PTR_set(@str, 2, 'C'.ord)
        @s.rb_str_set_len(@str, 3)

        @str.bytesize.should == 3
        @str.should == "aBC"
      end
    end
  end

  describe "rb_str_buf_new" do
    it "returns the equivalent of an empty string" do
      buf = @s.rb_str_buf_new(10, nil)
      buf.should == ""
      buf.bytesize.should == 0
      buf.size.should == 0
      @s.RSTRING_LEN(buf).should == 0
    end

    it "returns a string with the given capacity" do
      buf = @s.rb_str_buf_new(256, nil)
      @s.rb_str_capacity(buf).should == 256
    end

    it "returns a string that can be appended to" do
      str = @s.rb_str_buf_new(10, "defg")
      str << "abcde"
      str.should == "abcde"
    end

    it "returns a string that can be concatenated to another string" do
      str = @s.rb_str_buf_new(10, "defg")
      ("abcde" + str).should == "abcde"
    end

    it "returns a string whose bytes can be accessed by RSTRING_PTR" do
      str = @s.rb_str_buf_new(10, "abcdefghi")
      @s.rb_str_new(str, 10).should == "abcdefghi\x00"
    end

    it "returns a string that can be modified by rb_str_set_len" do
      str = @s.rb_str_buf_new(10, "abcdef")
      @s.rb_str_set_len(str, 4)
      str.should == "abcd"

      @s.rb_str_set_len(str, 8)
      str[0, 6].should == "abcd\x00f"
      @s.RSTRING_LEN(str).should == 8
    end

    it "can be used as a general buffer and reveal characters with rb_str_set_len" do
      str = @s.rb_str_buf_new(10, "abcdef")

      @s.RSTRING_PTR_set(str, 0, 195)
      @s.RSTRING_PTR_set(str, 1, 169)
      @s.rb_str_set_len(str, 2)

      str.force_encoding(Encoding::UTF_8)
      str.bytesize.should == 2
      str.size.should == 1
      str.should == "é"
    end
  end

  describe "rb_str_buf_new2" do
    it "returns a new string object calling strlen on the passed C string" do
      # Hardcoded to pass const char * = "hello\0invisible"
      @s.rb_str_buf_new2.should == "hello"
    end
  end

  describe "rb_str_new" do
    it "creates a new String with ASCII-8BIT Encoding" do
      @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT
    end

    it "returns a new string object from a char buffer of len characters" do
      @s.rb_str_new("hello", 3).should == "hel"
    end

    it "returns a non-tainted string" do
      @s.rb_str_new("hello", 5).tainted?.should == false
    end

    it "returns an empty string if len is 0" do
      @s.rb_str_new("hello", 0).should == ""
    end

    it "copy length bytes and does not stop at the first \\0 byte" do
      @s.rb_str_new("he\x00llo", 6).should == "he\x00llo"
      @s.rb_str_new_native("he\x00llo", 6).should == "he\x00llo"
{'fraction_non_alphanumeric': 0.11669789714761607, 'fraction_numerical': 0.01884239017280866, 'mean_word_length': 2.453271028037383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7292692', 'n_tokens_mistral': 3668, 'n_tokens_neox': 3455, 'n_words': 960}