ValueList uses new method

This commit is contained in:
Denis Molony 2017-03-18 13:22:49 +11:00
parent 914199601e
commit e090ca8525
5 changed files with 78 additions and 52 deletions

View File

@ -15,7 +15,7 @@ class And extends Function
while (true)
{
String parameter = Expression.getParameter (remainder);
System.out.printf ("cond: [%s]%n", parameter);
// System.out.printf ("cond: [%s]%n", parameter);
conditions.add (new Condition (parent, cell, parameter));
// System.out.printf (" [%s]%n", remainder);
// System.out.printf (" [%s]%n", parameter);

View File

@ -11,19 +11,26 @@ public class Choose extends Function
{
super (parent, cell, text);
int pos = functionText.indexOf (',');
sourceText = functionText.substring (0, pos);
// int pos = functionText.indexOf (',');
// sourceText = functionText.substring (0, pos);
sourceText = Expression.getParameter (functionText);
source = new Expression (parent, cell, sourceText).reduce ();
rangeText = functionText.substring (pos + 1);
range = new Range (parent, cell, rangeText);
values.add (source);
rangeText = functionText.substring (sourceText.length () + 1);
range = new Range (parent, cell, rangeText);
}
@Override
public void calculate ()
{
source.calculate ();
if (!source.isValueType (ValueType.VALUE))
{
valueType = source.getValueType ();
return;
}
int index = (int) source.getValue () - 1;
if (index < 0 || index >= range.size ())
{

View File

@ -15,7 +15,7 @@ class Or extends Function
while (true)
{
String parameter = Expression.getParameter (remainder);
System.out.printf ("cond: [%s]%n", parameter);
// System.out.printf ("cond: [%s]%n", parameter);
conditions.add (new Condition (parent, cell, parameter));
// System.out.printf (" [%s]%n", remainder);
// System.out.printf (" [%s]%n", parameter);

View File

@ -72,6 +72,11 @@ class Range implements Iterable<Address>
from = tempFrom;
}
static boolean isRange (String text)
{
return rangePattern.matcher (text).matches ();
}
boolean isHorizontal ()
{
return isHorizontal;

View File

@ -3,66 +3,80 @@ package com.bytezone.diskbrowser.visicalc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
public class ValueList implements Iterable<Value>
{
private static final Pattern cellAddress = Pattern.compile ("[A-B]?[A-Z][0-9]{1,3}");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
private final Sheet parent;
protected List<Value> values = new ArrayList<Value> ();
public ValueList (Sheet parent, Cell cell, String text)
{
this.parent = parent;
int ptr = 0;
while (ptr < text.length ())
String remainder = text;
while (true)
{
if (text.charAt (ptr) == '@') // function
{
String functionText = Expression.getBalancedText (text.substring (ptr));
Value v = new Expression (parent, cell, functionText).reduce ();
values.add (v);
ptr += functionText.length ();
}
String parameter = Expression.getParameter (remainder);
if (Range.isRange (parameter))
for (Address address : new Range (parent, cell, parameter))
values.add (parent.getCell (address));
else
{
String item = getNextItem (text, ptr);
int pos = item.indexOf ("...");
if (pos > 0) // range
{
Address from = new Address (item.substring (0, pos));
Address to = new Address (item.substring (pos + 3));
Range range = new Range (parent, from, to);
values.add (new Expression (parent, cell, parameter).reduce ());
for (Address address : range)
values.add (parent.getCell (address));
}
else // cell/number/expression
{
Value v = new Expression (parent, cell, item).reduce ();
values.add (v);
}
ptr += item.length ();
}
if (ptr < text.length () && text.charAt (ptr) == ',')
ptr++;
if (ptr < text.length () && text.charAt (ptr) == ')')
if (remainder.length () == parameter.length ())
break;
remainder = remainder.substring (parameter.length () + 1);
}
}
// public ValueList (Sheet parent, Cell cell, String text)
// {
// this.parent = parent;
//
// int ptr = 0;
// while (ptr < text.length ())
// {
// if (text.charAt (ptr) == '@') // function
// {
// String functionText = Expression.getBalancedText (text.substring (ptr));
// Value v = new Expression (parent, cell, functionText).reduce ();
// values.add (v);
// ptr += functionText.length ();
// }
// else
// {
// String item = getNextItem (text, ptr);
// int pos = item.indexOf ("...");
// if (pos > 0) // range
// {
// Address from = new Address (item.substring (0, pos));
// Address to = new Address (item.substring (pos + 3));
// Range range = new Range (parent, from, to);
//
// for (Address address : range)
// values.add (parent.getCell (address));
// }
// else // cell/number/expression
// {
// Value v = new Expression (parent, cell, item).reduce ();
// values.add (v);
// }
// ptr += item.length ();
// }
//
// if (ptr < text.length () && text.charAt (ptr) == ',')
// ptr++;
// if (ptr < text.length () && text.charAt (ptr) == ')')
// break;
// }
// }
// return substring of text from ptr up to the next comma
private String getNextItem (String text, int ptr)
{
int p = ptr;
while (++p < text.length () && text.charAt (p) != ',')
;
return text.substring (ptr, p);
}
// private String getNextItem (String text, int ptr)
// {
// int p = ptr;
// while (++p < text.length () && text.charAt (p) != ',')
// ;
// return text.substring (ptr, p);
// }
public int size ()
{