Ignore:
Timestamp:
Nov 5, 2005, 9:48:45 PM (20 years ago)
Author:
ggaren
Message:

Reviewed by Darin.

Modified String.slice to deal with funky values.
Updated test results. We now pass <js1_2/String/slice.js>.

  • kjs/string_object.cpp: (StringProtoFuncImp::callAsFunction):
  • tests/mozilla/expected.html:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/string_object.cpp

    r10818 r11077  
    511511  case Slice:
    512512    {
    513         // The arg processing is very much like ArrayProtoFunc::Slice
    514         double begin = args[0]->toInteger(exec);
    515         if (begin >= 0) { // false for NaN
    516           if (begin > len)
    517             begin = len;
    518         } else {
    519           begin += len;
    520           if (!(begin >= 0)) // true for NaN
    521             begin = 0;
    522         }
    523         double end = len;
    524         if (!args[1]->isUndefined()) {
    525           end = args[1]->toInteger(exec);
    526           if (end >= 0) { // false for NaN
    527             if (end > len)
    528               end = len;
    529           } else {
    530             end += len;
    531             if (!(end >= 0)) // true for NaN
    532               end = 0;
    533           }
    534         }
    535         //printf( "Slicing from %d to %d \n", begin, end );
    536         result = String(s.substr(static_cast<int>(begin), static_cast<int>(end-begin)));
    537         break;
     513      // The arg processing is very much like ArrayProtoFunc::Slice
     514      double start = a0->toInteger(exec);
     515      double end = a1->isUndefined() ? len : a1->toInteger(exec);
     516      double from = start < 0 ? len + start : start;
     517      double to = end < 0 ? len + end : end;
     518      if (to > from && to > 0 && from < len) {
     519        if (from < 0)
     520          from = 0;
     521        if (to > len)
     522          to = len;
     523        result = String(s.substr(static_cast<int>(from), static_cast<int>(to - from)));
     524      } else {
     525        result = String("");
     526      }
     527      break;
    538528    }
    539529    case Split: {
Note: See TracChangeset for help on using the changeset viewer.