Exemplo n.º 1
0
 @Override
 public void writeLines(RAbstractStringVector lines, String sep, boolean useBytes)
     throws IOException {
   for (int i = 0; i < lines.getLength(); i++) {
     String line = lines.getDataAt(i);
     outputStream.write(line.getBytes());
     outputStream.write(sep.getBytes());
   }
 }
Exemplo n.º 2
0
 protected String lookupName(RList f) {
   if (f.getNames() != null) {
     RAbstractStringVector names = f.getNames();
     for (int i = 0; i < names.getLength(); i++) {
       if (names.getDataAt(i).equals("name")) {
         String name = RRuntime.asString(f.getDataAt(i));
         return name != null ? name : UNKNOWN_EXTERNAL_BUILTIN;
       }
     }
   }
   return UNKNOWN_EXTERNAL_BUILTIN;
 }
Exemplo n.º 3
0
 @Override
 public void writeLines(RAbstractStringVector lines, String sep, boolean useBytes)
     throws IOException {
   // TODO encodings
   raf.seek(writeOffset);
   byte[] sepData = sep.getBytes();
   for (int i = 0; i < lines.getLength(); i++) {
     byte[] data = lines.getDataAt(i).getBytes();
     raf.write(data);
     raf.write(sepData);
   }
   writeOffset = raf.getFilePointer();
   lastMode = SeekRWMode.WRITE;
 }
 public String getDataAt(int index) {
   if (levels == null || levels.getLength() == 0) {
     return RRuntime.STRING_NA;
   } else {
     int val = ((RIntVector) vector).getDataAt(index);
     if (!vector.isComplete() && RRuntime.isNA(val)) {
       return RRuntime.STRING_NA;
     } else {
       String l = levels.getDataAt(val - 1);
       if (!levels.isComplete() && RRuntime.isNA(l)) {
         return "NA"; // for comparison
       } else {
         return l;
       }
     }
   }
 }
Exemplo n.º 5
0
 @Specialization
 @TruffleBoundary
 protected Object addCondHands(
     RAbstractStringVector classes,
     RList handlers,
     REnvironment parentEnv,
     Object target,
     byte calling) {
   if (classes.getLength() != handlers.getLength()) {
     throw RError.error(this, RError.Message.BAD_HANDLER_DATA);
   }
   return RErrorHandling.createHandlers(classes, handlers, parentEnv, target, calling);
 }
Exemplo n.º 6
0
 @Specialization
 protected RNull signalCondition(RList condition, RAbstractStringVector msg, Object call) {
   RErrorHandling.signalCondition(condition, msg.getDataAt(0), call);
   return RNull.instance;
 }
Exemplo n.º 7
0
  private static void printDimList(RAbstractListVector s, PrintContext printCtx)
      throws IOException {
    final PrintParameters pp = printCtx.parameters();

    int ns = s.getLength();
    String[] t = new String[ns];
    for (int i = 0; i < ns; i++) {
      Object tmp = RRuntime.asAbstractVector(s.getDataAtAsObject(i));
      final String pbuf;
      if (tmp == null || tmp == RNull.instance) {
        pbuf = RRuntime.NULL;
      } else if (tmp instanceof RAbstractLogicalVector) {
        RAbstractLogicalVector lv = (RAbstractLogicalVector) tmp;
        if (lv.getLength() == 1) {
          FormatMetrics fm = LogicalVectorPrinter.formatLogicalVector(lv, 0, 1, pp.getNaWidth());
          pbuf =
              snprintf(
                  115, "%s", LogicalVectorPrinter.encodeLogical(lv.getDataAt(0), fm.maxWidth, pp));
        } else {
          pbuf = snprintf(115, "Logical,%d", lv.getLength());
        }
      } else if (tmp instanceof RAbstractIntVector) {
        RAbstractIntVector iv = (RAbstractIntVector) tmp;
        if (printCtx.printerNode().inherits(iv, RRuntime.CLASS_FACTOR)) {
          /* factors are stored as integers */
          pbuf = snprintf(115, "factor,%d", iv.getLength());
        } else {
          if (iv.getLength() == 1) {
            FormatMetrics fm = IntegerVectorPrinter.formatIntVector(iv, 0, 1, pp.getNaWidth());
            pbuf =
                snprintf(
                    115,
                    "%s",
                    IntegerVectorPrinter.encodeInteger(iv.getDataAt(0), fm.maxWidth, pp));
          } else {
            pbuf = snprintf(115, "Integer,%d", iv.getLength());
          }
        }
      } else if (tmp instanceof RAbstractDoubleVector) {
        RAbstractDoubleVector dv = (RAbstractDoubleVector) tmp;
        if (dv.getLength() == 1) {
          DoubleVectorMetrics fm = DoubleVectorPrinter.formatDoubleVector(dv, 0, 1, 0, pp);
          pbuf = snprintf(115, "%s", DoubleVectorPrinter.encodeReal(dv.getDataAt(0), fm, pp));
        } else {
          pbuf = snprintf(115, "Numeric,%d", dv.getLength());
        }
      } else if (tmp instanceof RAbstractComplexVector) {
        RAbstractComplexVector cv = (RAbstractComplexVector) tmp;
        if (cv.getLength() == 1) {
          RComplex x = cv.getDataAt(0);
          if (RRuntime.isNA(x.getRealPart()) || RRuntime.isNA(x.getImaginaryPart())) {
            /* formatReal(NA) --> w=R_print.na_width, d=0, e=0 */
            pbuf =
                snprintf(
                    115,
                    "%s",
                    DoubleVectorPrinter.encodeReal(
                        RRuntime.DOUBLE_NA, pp.getNaWidth(), 0, 0, '.', pp));
          } else {
            ComplexVectorMetrics cvm = ComplexVectorPrinter.formatComplexVector(x, 0, 1, 0, pp);
            pbuf = snprintf(115, "%s", ComplexVectorPrinter.encodeComplex(x, cvm, pp));
          }
        } else {
          pbuf = snprintf(115, "Complex,%d", cv.getLength());
        }
      } else if (tmp instanceof RAbstractStringVector) {
        RAbstractStringVector sv = (RAbstractStringVector) tmp;
        if (sv.getLength() == 1) {
          String ctmp = sv.getDataAt(0);
          int len = ctmp.length();
          if (len < 100) {
            pbuf = snprintf(115, "\"%s\"", ctmp);
          } else {
            pbuf = snprintf(101, "\"%s", ctmp) + "\" [truncated]";
          }
        } else {
          pbuf = snprintf(115, "Character,%d", sv.getLength());
        }
      } else if (tmp instanceof RAbstractRawVector) {
        pbuf = snprintf(115, "Raw,%d", ((RAbstractRawVector) (tmp)).getLength());
      } else if (tmp instanceof RAbstractListVector) {
        pbuf = snprintf(115, "List,%d", ((RAbstractListVector) (tmp)).getLength());
      } else if (tmp instanceof RLanguage) {
        pbuf = snprintf(115, "Expression");
      } else {
        pbuf = snprintf(115, "?");
      }

      t[i] = pbuf;
    }

    RStringVector tt = RDataFactory.createStringVector(t, true, s.getDimensions());
    Object dimNames = s.getAttr(RRuntime.DIMNAMES_ATTR_KEY);
    tt.setAttr(RRuntime.DIMNAMES_ATTR_KEY, dimNames);

    PrintContext cc = printCtx.cloneContext();
    cc.parameters().setQuote(false);
    StringVectorPrinter.INSTANCE.print(tt, cc);
  }
Exemplo n.º 8
0
  @TruffleBoundary
  static void printNoDimList(RAbstractContainer s, PrintContext printCtx) throws IOException {
    final PrintParameters pp = printCtx.parameters();
    final PrintWriter out = printCtx.output();

    final StringBuilder tagbuf = printCtx.getOrCreateTagBuffer();
    // save the original length so that we can restore the original value
    int taglen = tagbuf.length();

    int ns = s.getLength();

    RAbstractStringVector names;
    names = Utils.castTo(RRuntime.asAbstractVector(s.getNames(dummyAttrProfiles)));

    if (ns > 0) {
      int npr = (ns <= pp.getMax() + 1) ? ns : pp.getMax();
      /* '...max +1' ==> will omit at least 2 ==> plural in msg below */
      for (int i = 0; i < npr; i++) {
        if (i > 0) {
          out.println();
        }
        String ss = names == null ? null : Utils.<String>getDataAt(names, i);
        if (ss != null && !ss.isEmpty()) {
          /*
           * Bug for L <- list(`a\\b` = 1, `a\\c` = 2) : const char *ss =
           * translateChar(STRING_ELT(names, i));
           */
          if (taglen + ss.length() > TAGBUFLEN) {
            if (taglen <= TAGBUFLEN) {
              tagbuf.append("$...");
            }
          } else {
            /*
             * we need to distinguish character NA from "NA", which is a valid (if
             * non-syntactic) name
             */
            if (ss == RRuntime.STRING_NA) {
              tagbuf.append("$<NA>");
            } else if (RDeparse.isValidName(ss)) {
              tagbuf.append(String.format("$%s", ss));
            } else {
              tagbuf.append(String.format("$`%s`", ss));
            }
          }

        } else {
          if (taglen + indexWidth(i) > TAGBUFLEN) {
            if (taglen <= TAGBUFLEN) {
              tagbuf.append("$...");
            }
          } else {
            tagbuf.append(String.format("[[%d]]", i + 1));
          }
        }

        out.println(tagbuf);
        Object si = s.getDataAtAsObject(i);
        if (si instanceof RAttributable && ((RAttributable) si).isObject()) {
          RContext.getEngine().printResult(si);
        } else {
          ValuePrinters.INSTANCE.print(si, printCtx);
          ValuePrinters.printNewLine(printCtx);
        }
        tagbuf.setLength(taglen); // reset tag buffer to the original value
      }

      if (npr < ns) {
        out.printf("\n [ reached getOption(\"max.print\") -- omitted %d entries ]", ns - npr);
      }

    } else {
      /* ns = length(s) == 0 */

      /* Formal classes are represented as empty lists */
      String className = null;
      if (printCtx.printerNode().isObject(s) && printCtx.printerNode().isMethodDispatchOn()) {
        RAbstractStringVector klass =
            Utils.castTo(RRuntime.asAbstractVector(s.getAttr(RRuntime.CLASS_ATTR_KEY)));
        if (klass != null && klass.getLength() == 1) {
          String ss = klass.getDataAt(0);
          String str = snprintf(200, ".__C__%s", ss);
          Frame frame = com.oracle.truffle.r.runtime.Utils.getActualCurrentFrame();
          if (ReadVariableNode.lookupAny(str, frame, false) != null) {
            className = ss;
          }
        }
      }
      if (className != null) {
        out.printf("An object of class \"%s\"", className);
      } else {
        if (names != null) {
          out.print("named ");
        }
        out.print("list()");
      }
    }
  }