1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import jdk.nashorn.internal.objects.annotations.Function;
  29 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  30 import jdk.nashorn.internal.runtime.PropertyMap;
  31 import jdk.nashorn.internal.runtime.ScriptRuntime;
  32 import jdk.nashorn.internal.runtime.Undefined;
  33 
  34 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
  35 
  36 /**
  37  * ECMA6 21.1.5 String Iterator Objects
  38  */
  39 @ScriptClass("StringIterator")
  40 public class StringIterator extends AbstractIterator {
  41 
  42     // initialized by nasgen
  43     private static PropertyMap $nasgenmap$;
  44 
  45     private String iteratedString;
  46     private int nextIndex = 0;
  47     private final Global global;
  48 
  49     StringIterator(final String iteratedString, final Global global) {
  50         super(global.getStringIteratorPrototype(), $nasgenmap$);
  51         this.iteratedString = iteratedString;
  52         this.global = global;
  53     }
  54 
  55     /**
  56      * ES6 21.1.5.2.1 %StringIteratorPrototype%.next()
  57      *
  58      * @param self the self reference
  59      * @param arg the argument
  60      * @return the next result
  61      */
  62     @Function
  63     public static Object next(final Object self, final Object arg) {
  64         if (!(self instanceof StringIterator)) {
  65             throw typeError("not.a.string.iterator", ScriptRuntime.safeToString(self));
  66         }
  67         return ((StringIterator)self).next(arg);
  68     }
  69 
  70     @Override
  71     public String getClassName() {
  72         return "String Iterator";
  73     }
  74 
  75     @Override
  76     protected IteratorResult next(final Object arg) {
  77         final int index = nextIndex;
  78         final String string = iteratedString;
  79 
  80         if (string == null || index >= string.length()) {
  81             // ES6 21.1.5.2.1 step 8
  82             iteratedString = null;
  83             return makeResult(Undefined.getUndefined(), Boolean.TRUE, global);
  84         }
  85 
  86         final char first = string.charAt(index);
  87         if (first >= 0xd800 && first <= 0xdbff && index < string.length() - 1) {
  88             final char second = string.charAt(index + 1);
  89             if (second >= 0xdc00 && second <= 0xdfff) {
  90                 nextIndex += 2;
  91                 return makeResult(String.valueOf(new char[] {first, second}), Boolean.FALSE, global);
  92             }
  93         }
  94 
  95         nextIndex++;
  96         return makeResult(String.valueOf(first), Boolean.FALSE, global);
  97     }
  98 }