Ignore:
Timestamp:
Jan 21, 2018, 12:10:09 AM (6 years ago)
Author:
coas-nagasima
Message:

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

Location:
EcnlProtoTool/trunk/mruby-1.3.0
Files:
4 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-string-ext/mrbgem.rake

    r321 r331  
    33  spec.author  = 'mruby developers'
    44  spec.summary = 'String class extension'
     5  spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator'
    56end
  • EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-string-ext/mrblib/string.rb

    r321 r331  
    11class String
     2
     3  ##
     4  #  call-seq:
     5  #     String.try_convert(obj) -> string or nil
     6  #
     7  # Try to convert <i>obj</i> into a String, using to_str method.
     8  # Returns converted string or nil if <i>obj</i> cannot be converted
     9  # for any reason.
     10  #
     11  #     String.try_convert("str")     #=> "str"
     12  #     String.try_convert(/re/)      #=> nil
     13  #
     14  def self.try_convert(obj)
     15    if obj.respond_to?(:to_str)
     16      obj.to_str
     17    else
     18      nil
     19    end
     20  end
    221
    322  ##
     
    2746    a = 0
    2847    z = self.size - 1
    29     a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
     48    a += 1 while a <= z and " \f\n\r\t\v".include?(self[a])
    3049    (z >= 0) ? self[a..z] : ""
    3150  end
     
    4463    a = 0
    4564    z = self.size - 1
    46     z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z
     65    z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z])
    4766    (z >= 0) ? self[a..z] : ""
    4867  end
     
    6079    a = 0
    6180    z = self.size - 1
    62     a += 1 while " \f\n\r\t\v".include?(self[a]) and a <= z
    63     z -= 1 while " \f\n\r\t\v\0".include?(self[z]) and a <= z
     81    a += 1 while a <= z and " \f\n\r\t\v".include?(self[a])
     82    z -= 1 while a <= z and " \f\n\r\t\v\0".include?(self[z])
    6483    (z >= 0) ? self[a..z] : ""
    6584  end
     
    7796  #
    7897  def lstrip!
     98    raise RuntimeError, "can't modify frozen String" if frozen?
    7999    s = self.lstrip
    80100    (s == self) ? nil : self.replace(s)
     
    93113  #
    94114  def rstrip!
     115    raise RuntimeError, "can't modify frozen String" if frozen?
    95116    s = self.rstrip
    96117    (s == self) ? nil : self.replace(s)
     
    105126  #
    106127  def strip!
     128    raise RuntimeError, "can't modify frozen String" if frozen?
    107129    s = self.strip
    108130    (s == self) ? nil : self.replace(s)
     
    165187  #
    166188  def slice!(arg1, arg2=nil)
     189    raise RuntimeError, "can't modify frozen String" if frozen?
    167190    raise "wrong number of arguments (for 1..2)" if arg1.nil? && arg2.nil?
    168191
     
    189212        idx = arg1
    190213        idx += self.size if arg1 < 0
    191         validated = true if idx >=0 && arg1 < self.size   
     214        validated = true if idx >=0 && arg1 < self.size
    192215      end
    193216      if validated
     
    236259  #
    237260  def insert(idx, str)
    238     pos = idx.to_i
    239     pos += self.size + 1 if pos < 0
    240 
    241     raise IndexError, "index #{idx.to_i} out of string" if pos < 0 || pos > self.size
    242 
    243     return self + str if pos == -1
    244     return str + self if pos == 0
    245     return self[0..pos - 1] + str + self[pos..-1]
     261    if idx == -1
     262      return self << str
     263    elsif idx < 0
     264      idx += 1
     265    end
     266    self[idx, 0] = str
     267    self
    246268  end
    247269
     
    258280  #     "hello".ljust(20, '1234')   #=> "hello123412341234123"
    259281  def ljust(idx, padstr = ' ')
    260     if idx <= self.size
    261       return self
    262     end
    263     newstr = self.dup
    264     newstr << padstr
    265     while newstr.size <= idx
    266       newstr << padstr
    267     end
    268     return newstr.slice(0,idx)
    269   end
    270 
    271   #     str.upto(other_str, exclusive=false) {|s| block }   -> str
    272   #     str.upto(other_str, exclusive=false)                -> an_enumerator
    273   #
    274   #  Iterates through successive values, starting at <i>str</i> and
    275   #  ending at <i>other_str</i> inclusive, passing each value in turn to
    276   #  the block. The <code>String#succ</code> method is used to generate
    277   #  each value.  If optional second argument exclusive is omitted or is false,
    278   #  the last value will be included; otherwise it will be excluded.
    279   #
    280   #  If no block is given, an enumerator is returned instead.
    281   #
    282   #     "a8".upto("b6") {|s| print s, ' ' }
    283   #     for s in "a8".."b6"
    284   #       print s, ' '
    285   #     end
    286   #
    287   #  <em>produces:</em>
    288   #
    289   #     a8 a9 b0 b1 b2 b3 b4 b5 b6
    290   #     a8 a9 b0 b1 b2 b3 b4 b5 b6
    291   #
    292   #  If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
    293   #  both are recognized as decimal numbers. In addition, the width of
    294   #  string (e.g. leading zeros) is handled appropriately.
    295   #
    296   #     "9".upto("11").to_a   #=> ["9", "10", "11"]
    297   #     "25".upto("5").to_a   #=> []
    298   #     "07".upto("11").to_a  #=> ["07", "08", "09", "10", "11"]
    299   #
    300   def upto(other_str, excl=false, &block)
    301     return to_enum :upto, other_str, excl unless block
    302 
    303     str = self
    304     n = self.<=>other_str
    305     return self if n > 0 || (self == other_str && excl)
    306     while true
    307       block.call(str)
    308       return self if !excl && str == other_str
    309       str = str.succ
    310       return self if excl && str == other_str
    311     end
     282    raise ArgumentError, 'zero width padding' if padstr == ''
     283    return self if idx <= self.size
     284    pad_repetitions = (idx / padstr.length).ceil
     285    padding = (padstr * pad_repetitions)[0...(idx - self.length)]
     286    self + padding
     287  end
     288
     289  ##
     290  #  call-seq:
     291  #     str.rjust(integer, padstr=' ')   -> new_str
     292  #
     293  #  If <i>integer</i> is greater than the length of <i>str</i>, returns a new
     294  #  <code>String</code> of length <i>integer</i> with <i>str</i> right justified
     295  #  and padded with <i>padstr</i>; otherwise, returns <i>str</i>.
     296  #
     297  #     "hello".rjust(4)            #=> "hello"
     298  #     "hello".rjust(20)           #=> "               hello"
     299  #     "hello".rjust(20, '1234')   #=> "123412341234123hello"
     300  def rjust(idx, padstr = ' ')
     301    raise ArgumentError, 'zero width padding' if padstr == ''
     302    return self if idx <= self.size
     303    pad_repetitions = (idx / padstr.length).ceil
     304    padding = (padstr * pad_repetitions)[0...(idx - self.length)]
     305    padding + self
    312306  end
    313307
    314308  def chars(&block)
    315309    if block_given?
    316       self.split('').map do |i|
     310      self.split('').each do |i|
    317311        block.call(i)
    318312      end
     
    322316    end
    323317  end
    324   alias each_char chars
     318
     319  def each_char(&block)
     320    return to_enum :each_char unless block
     321
     322    split('').each do |i|
     323      block.call(i)
     324    end
     325    self
     326  end
    325327
    326328  def codepoints(&block)
     
    328330
    329331    if block_given?
    330       self.split('').map do|x|
     332      self.split('').each do|x|
    331333        block.call(x.ord)
    332334      end
     
    337339  end
    338340  alias each_codepoint codepoints
     341
     342  ##
     343  # call-seq:
     344  #    str.prepend(other_str)  -> str
     345  #
     346  # Prepend---Prepend the given string to <i>str</i>.
     347  #
     348  #    a = "world"
     349  #    a.prepend("hello ") #=> "hello world"
     350  #    a                   #=> "hello world"
     351  def prepend(arg)
     352    self[0, 0] = arg
     353    self
     354  end
    339355end
  • EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-string-ext/src/string.c

    r321 r331  
    11#include <string.h>
    2 #include "mruby.h"
    3 #include "mruby/array.h"
    4 #include "mruby/class.h"
    5 #include "mruby/string.h"
    6 #include "mruby/range.h"
     2#include <mruby.h>
     3#include <mruby/array.h>
     4#include <mruby/class.h>
     5#include <mruby/string.h>
     6#include <mruby/range.h>
    77
    88static mrb_value
     
    2424{
    2525  mrb_int pos, byte;
    26   long len = RSTRING_LEN(str);
     26  long len;
    2727
    2828  mrb_get_args(mrb, "ii", &pos, &byte);
    2929
     30  len = RSTRING_LEN(str);
    3031  if (pos < -len || len <= pos)
    3132    mrb_raisef(mrb, E_INDEX_ERROR, "index %S is out of array", mrb_fixnum_value(pos));
     
    5657
    5758      len = RSTRING_LEN(str);
    58       if (mrb_range_beg_len(mrb, a1, &beg, &len, len)) {
     59      switch (mrb_range_beg_len(mrb, a1, &beg, &len, len, TRUE)) {
     60      case 0:                   /* not range */
     61        break;
     62      case 1:                   /* range */
    5963        return mrb_str_substr(mrb, str, beg, len);
     64      case 2:                   /* out of range */
     65        mrb_raisef(mrb, E_RANGE_ERROR, "%S out of range", a1);
     66        break;
    6067      }
    6168      return mrb_nil_value();
     
    128135}
    129136
     137static mrb_value mrb_fixnum_chr(mrb_state *mrb, mrb_value num);
     138
    130139/*
    131140 *  call-seq:
     
    147156{
    148157  mrb_value str;
    149   mrb_get_args(mrb, "S", &str);
     158
     159  mrb_get_args(mrb, "o", &str);
     160  if (mrb_fixnum_p(str))
     161    str = mrb_fixnum_chr(mrb, str);
     162  else
     163    str = mrb_string_type(mrb, str);
    150164  mrb_str_concat(mrb, self, str);
    151165  return self;
     
    308322  mrb_int len;
    309323  mrb_value arg;
    310   char *p = RSTRING_PTR(self), *t;
    311   char *e = p + RSTRING_LEN(self);
     324  char *b = RSTRING_PTR(self);
     325  char *p = b, *t;
     326  char *e = b + RSTRING_LEN(self);
    312327
    313328  mrb_get_args(mrb, "&", &blk);
    314329
    315330  result = mrb_ary_new(mrb);
    316 
     331  ai = mrb_gc_arena_save(mrb);
    317332  if (!mrb_nil_p(blk)) {
    318333    while (p < e) {
     
    323338      arg = mrb_str_new(mrb, t, len);
    324339      mrb_yield_argv(mrb, blk, 1, &arg);
     340      mrb_gc_arena_restore(mrb, ai);
     341      if (b != RSTRING_PTR(self)) {
     342        ptrdiff_t diff = p - b;
     343        b = RSTRING_PTR(self);
     344        p = b + diff;
     345      }
     346      e = b + RSTRING_LEN(self);
    325347    }
    326348    return self;
    327349  }
    328350  while (p < e) {
    329     ai = mrb_gc_arena_save(mrb);
    330351    t = p;
    331352    while (p < e && *p != '\n') p++;
     
    354375  const char *prepend;
    355376  struct RString *s = mrb_str_ptr(self);
    356   size_t l;
     377  mrb_int l;
    357378
    358379  if (RSTRING_LEN(self) == 0)
     
    374395    e = p + l - 1;
    375396    result = mrb_str_new_lit(mrb, "");
    376   } else {
     397  }
     398  else {
    377399    // find leading letter of the ascii/number
    378400    b = e;
     
    392414        mrb_str_cat_lit(mrb, result, "\x01");
    393415        (*e) = 0;
    394       } else
     416      }
     417      else
    395418        (*e)++;
    396419      break;
     
    400423      if (e == b) prepend = "1";
    401424      *e = '0';
    402     } else if (*e == 'z') {
     425    }
     426    else if (*e == 'z') {
    403427      if (e == b) prepend = "a";
    404428      *e = 'a';
    405     } else if (*e == 'Z') {
     429    }
     430    else if (*e == 'Z') {
    406431      if (e == b) prepend = "A";
    407432      *e = 'A';
    408     } else {
     433    }
     434    else {
    409435      (*e)++;
    410436      break;
     
    428454  mrb_str_succ_bang(mrb, str);
    429455  return str;
    430 }
    431 
    432 /*
    433  *  call-seq:
    434  *     str.prepend(other_str)  -> str
    435  *
    436  *  Prepend---Prepend the given string to <i>str</i>.
    437  *
    438  *     a = "world"
    439  *     a.prepend("hello ") #=> "hello world"
    440  *     a                   #=> "hello world"
    441  */
    442 static mrb_value
    443 mrb_str_prepend(mrb_state *mrb, mrb_value self)
    444 {
    445   struct RString *s1 = mrb_str_ptr(self), *s2, *temp_s;
    446   mrb_int len;
    447   mrb_value other, temp_str;
    448 
    449   mrb_get_args(mrb, "S", &other);
    450 
    451   mrb_str_modify(mrb, s1);
    452   if (!mrb_string_p(other)) {
    453     other = mrb_str_to_str(mrb, other);
    454   }
    455   s2 = mrb_str_ptr(other);
    456   len = RSTR_LEN(s1) + RSTR_LEN(s2);
    457   temp_str = mrb_str_new(mrb, NULL, RSTR_LEN(s1));
    458   temp_s = mrb_str_ptr(temp_str);
    459   memcpy(RSTR_PTR(temp_s), RSTR_PTR(s1), RSTR_LEN(s1));
    460   if (RSTRING_CAPA(self) < len) {
    461     mrb_str_resize(mrb, self, len);
    462   }
    463   memcpy(RSTR_PTR(s1), RSTR_PTR(s2), RSTR_LEN(s2));
    464   memcpy(RSTR_PTR(s1) + RSTR_LEN(s2), RSTR_PTR(temp_s), RSTR_LEN(temp_s));
    465   RSTR_SET_LEN(s1, len);
    466   RSTR_PTR(s1)[len] = '\0';
    467   return self;
    468456}
    469457
     
    530518  if (RSTRING_LEN(str) == 0)
    531519    mrb_raise(mrb, E_ARGUMENT_ERROR, "empty string");
    532   return mrb_fixnum_value(RSTRING_PTR(str)[0]);
     520  return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[0]);
    533521}
    534522#endif
     523
     524static mrb_bool
     525all_digits_p(const char *s, mrb_int len)
     526{
     527  while (len-- > 0) {
     528    if (!ISDIGIT(*s)) return FALSE;
     529    s++;
     530  }
     531  return TRUE;
     532}
     533
     534/*
     535 *  call-seq:
     536 *     str.upto(other_str, exclusive=false) {|s| block }   -> str
     537 *     str.upto(other_str, exclusive=false)                -> an_enumerator
     538 *
     539 *  Iterates through successive values, starting at <i>str</i> and
     540 *  ending at <i>other_str</i> inclusive, passing each value in turn to
     541 *  the block. The <code>String#succ</code> method is used to generate
     542 *  each value.  If optional second argument exclusive is omitted or is false,
     543 *  the last value will be included; otherwise it will be excluded.
     544 *
     545 *  If no block is given, an enumerator is returned instead.
     546 *
     547 *     "a8".upto("b6") {|s| print s, ' ' }
     548 *     for s in "a8".."b6"
     549 *       print s, ' '
     550 *     end
     551 *
     552 *  <em>produces:</em>
     553 *
     554 *     a8 a9 b0 b1 b2 b3 b4 b5 b6
     555 *     a8 a9 b0 b1 b2 b3 b4 b5 b6
     556 *
     557 *  If <i>str</i> and <i>other_str</i> contains only ascii numeric characters,
     558 *  both are recognized as decimal numbers. In addition, the width of
     559 *  string (e.g. leading zeros) is handled appropriately.
     560 *
     561 *     "9".upto("11").to_a   #=> ["9", "10", "11"]
     562 *     "25".upto("5").to_a   #=> []
     563 *     "07".upto("11").to_a  #=> ["07", "08", "09", "10", "11"]
     564 */
     565static mrb_value
     566mrb_str_upto(mrb_state *mrb, mrb_value beg)
     567{
     568  mrb_value end;
     569  mrb_value exclusive = mrb_false_value();
     570  mrb_value block = mrb_nil_value();
     571  mrb_value current, after_end;
     572  mrb_int n;
     573  mrb_bool excl;
     574
     575  mrb_get_args(mrb, "o|o&", &end, &exclusive, &block);
     576
     577  if (mrb_nil_p(block)) {
     578    return mrb_funcall(mrb, beg, "to_enum", 3, mrb_symbol_value(mrb_intern_lit(mrb, "upto")), end, exclusive);
     579  }
     580  end = mrb_string_type(mrb, end);
     581  excl = mrb_test(exclusive);
     582
     583  /* single character */
     584  if (RSTRING_LEN(beg) == 1 && RSTRING_LEN(end) == 1 &&
     585  ISASCII(RSTRING_PTR(beg)[0]) && ISASCII(RSTRING_PTR(end)[0])) {
     586    char c = RSTRING_PTR(beg)[0];
     587    char e = RSTRING_PTR(end)[0];
     588    int ai = mrb_gc_arena_save(mrb);
     589
     590    if (c > e || (excl && c == e)) return beg;
     591    for (;;) {
     592      mrb_yield(mrb, block, mrb_str_new(mrb, &c, 1));
     593      mrb_gc_arena_restore(mrb, ai);
     594      if (!excl && c == e) break;
     595      c++;
     596      if (excl && c == e) break;
     597    }
     598    return beg;
     599  }
     600  /* both edges are all digits */
     601  if (ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0]) &&
     602      all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) &&
     603      all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) {
     604    int ai = mrb_gc_arena_save(mrb);
     605    mrb_int min_width = RSTRING_LEN(beg);
     606    mrb_int max_width = RSTRING_LEN(end);
     607    mrb_int bi = mrb_int(mrb, mrb_str_to_inum(mrb, beg, 10, FALSE));
     608    mrb_int ei = mrb_int(mrb, mrb_str_to_inum(mrb, end, 10, FALSE));
     609    mrb_value str = mrb_str_new(mrb, NULL, max_width);
     610    char *buf = RSTRING_PTR(str);
     611
     612    while (bi <= ei) {
     613      if (excl && bi == ei) break;
     614      snprintf(buf, max_width+1, "%.*" MRB_PRId, (int)min_width, bi);
     615      mrb_yield(mrb, block, mrb_str_new(mrb, buf, strlen(buf)));
     616      mrb_gc_arena_restore(mrb, ai);
     617      bi++;
     618    }
     619
     620    return beg;
     621  }
     622  /* normal case */
     623  n = mrb_int(mrb, mrb_funcall(mrb, beg, "<=>", 1, end));
     624  if (n > 0 || (excl && n == 0)) return beg;
     625
     626  after_end = mrb_funcall(mrb, end, "succ", 0);
     627  current = mrb_str_dup(mrb, beg);
     628  while (!mrb_str_equal(mrb, current, after_end)) {
     629    int ai = mrb_gc_arena_save(mrb);
     630    mrb_value next = mrb_nil_value();
     631    if (excl || !mrb_str_equal(mrb, current, end))
     632      next = mrb_funcall(mrb, current, "succ", 0);
     633    mrb_yield(mrb, block, current);
     634    if (mrb_nil_p(next)) break;
     635    current = mrb_str_to_str(mrb, next);
     636    if (excl && mrb_str_equal(mrb, current, end)) break;
     637    if (RSTRING_LEN(current) > RSTRING_LEN(end) || RSTRING_LEN(current) == 0)
     638      break;
     639    mrb_gc_arena_restore(mrb, ai);
     640  }
     641
     642  return beg;
     643}
    535644
    536645void
     
    555664  mrb_define_method(mrb, s, "succ",            mrb_str_succ,            MRB_ARGS_NONE());
    556665  mrb_define_method(mrb, s, "succ!",           mrb_str_succ_bang,       MRB_ARGS_NONE());
    557   mrb_define_method(mrb, s, "prepend",         mrb_str_prepend,         MRB_ARGS_REQ(1));
    558666  mrb_alias_method(mrb, s, mrb_intern_lit(mrb, "next"), mrb_intern_lit(mrb, "succ"));
    559667  mrb_alias_method(mrb, s, mrb_intern_lit(mrb, "next!"), mrb_intern_lit(mrb, "succ!"));
    560668  mrb_define_method(mrb, s, "ord", mrb_str_ord, MRB_ARGS_NONE());
     669  mrb_define_method(mrb, s, "upto", mrb_str_upto, MRB_ARGS_ANY());
    561670
    562671  mrb_define_method(mrb, mrb->fixnum_class, "chr", mrb_fixnum_chr, MRB_ARGS_NONE());
  • EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-string-ext/test/string.rb

    r321 r331  
    33
    44UTF8STRING = ("\343\201\202".size == 1)
     5
     6assert('String.try_convert') do
     7  assert_nil String.try_convert(nil)
     8  assert_nil String.try_convert(:foo)
     9  assert_equal "", String.try_convert("")
     10  assert_equal "1,2,3", String.try_convert("1,2,3")
     11end
    512
    613assert('String#getbyte') do
     
    2229  assert_equal(h, str1.getbyte(0))
    2330  assert_equal("Hello", str1)
     31end
     32
     33assert("String#setbyte raises IndexError if arg conversion resizes String") do
     34  $s = "01234\n"
     35  class Tmp
     36      def to_i
     37          $s.chomp! ''
     38          95
     39      end
     40  end
     41  tmp = Tmp.new
     42  assert_raise(IndexError) { $s.setbyte(5, tmp) }
    2443end
    2544
     
    104123
    105124assert('String#concat') do
    106   s = "Hello "
    107   s.concat "World!"
    108   t = "Hello "
    109   t << "World!"
    110   assert_equal "Hello World!", t
    111   assert_equal "Hello World!", s
     125  assert_equal "Hello World!", "Hello " << "World" << 33
     126  assert_equal "Hello World!", "Hello ".concat("World").concat(33)
     127
     128  o = Object.new
     129  def o.to_str
     130    "to_str"
     131  end
     132  assert_equal "hi to_str", "hi " << o
     133
     134  assert_raise(TypeError) { "".concat(Object.new) }
    112135end
    113136
     
    400423  assert_raise(IndexError) { "abcd".insert(5, 'X') }
    401424  assert_raise(IndexError) { "abcd".insert(-6, 'X') }
     425
     426  a = "abcd"
     427  a.insert(0, 'X')
     428  assert_equal "Xabcd", a
    402429end
    403430
     
    411438  assert_equal "hello", "hello".ljust(4)
    412439  assert_equal "hello               ", "hello".ljust(20)
     440  assert_equal 20, "hello".ljust(20).length
    413441  assert_equal "hello123412341234123", "hello".ljust(20, '1234')
    414442  assert_equal "hello", "hello".ljust(-3)
    415443end
    416444
     445assert('String#rjust') do
     446  assert_equal "hello", "hello".rjust(4)
     447  assert_equal "               hello", "hello".rjust(20)
     448  assert_equal 20, "hello".rjust(20).length
     449  assert_equal "123412341234123hello", "hello".rjust(20, '1234')
     450  assert_equal "hello", "hello".rjust(-3)
     451end
     452
     453if UTF8STRING
     454  assert('String#ljust with UTF8') do
     455    assert_equal "helloん              ", "helloん".ljust(20)
     456    assert_equal "helloó                            ", "helloó".ljust(34)
     457    assert_equal 34, "helloó".ljust(34).length
     458    assert_equal "helloんんんんんんんんんんんんんん", "hello".ljust(19, 'ん')
     459    assert_equal "helloんんんんんんんんんんんんんんん", "hello".ljust(20, 'ん')
     460  end
     461
     462  assert('String#rjust with UTF8') do
     463    assert_equal "              helloん", "helloん".rjust(20)
     464    assert_equal "                            helloó", "helloó".rjust(34)
     465    # assert_equal 34, "helloó".rjust(34).length
     466    assert_equal "んんんんんんんんんんんんんんhello", "hello".rjust(19, 'ん')
     467    assert_equal "んんんんんんんんんんんんんんんhello", "hello".rjust(20, 'ん')
     468  end
     469
     470  assert('UTF8 byte counting') do
     471    ret = '                                  '
     472    ret[-6..-1] = "helloó"
     473    assert_equal 34, ret.length
     474  end
     475end
     476
     477assert('String#ljust should not change string') do
     478  a = "hello"
     479  a.ljust(20)
     480  assert_equal "hello", a
     481end
     482
     483assert('String#rjust should not change string') do
     484  a = "hello"
     485  a.rjust(20)
     486  assert_equal "hello", a
     487end
     488
     489assert('String#ljust should raise on zero width padding') do
     490  assert_raise(ArgumentError) { "foo".ljust(10, '') }
     491end
     492
     493assert('String#rjust should raise on zero width padding') do
     494  assert_raise(ArgumentError) { "foo".rjust(10, '') }
     495end
     496
    417497assert('String#upto') do
     498  assert_equal %w(a8 a9 b0 b1 b2 b3 b4 b5 b6), "a8".upto("b6").to_a
     499  assert_equal ["9", "10", "11"], "9".upto("11").to_a
     500  assert_equal [], "25".upto("5").to_a
     501  assert_equal ["07", "08", "09", "10", "11"], "07".upto("11").to_a
     502
     503if UTF8STRING
     504  assert_equal ["あ", "ぃ", "い", "ぅ", "う", "ぇ", "え", "ぉ", "お"], "あ".upto("お").to_a
     505end
     506
     507  assert_equal ["9", ":", ";", "<", "=", ">", "?", "@", "A"], "9".upto("A").to_a
     508
    418509  a     = "aa"
    419510  start = "aa"
     
    475566  })
    476567  assert_equal(2, count)
     568
     569  assert_raise(TypeError) { "a".upto(:c) {} }
    477570end
    478571
     
    480573  got = "hello!".split('').map {|x| x.ord}
    481574  expect = [104, 101, 108, 108, 111, 33]
     575  unless UTF8STRING
     576    got << "\xff".ord
     577    expect << 0xff
     578  end
    482579  assert_equal expect, got
    483580end
Note: See TracChangeset for help on using the changeset viewer.