Skip to content

Commit 701e7e6

Browse files
hansonrhansonr
authored andcommitted
javajs/util for CifDataParser and DF
1 parent e2149c1 commit 701e7e6

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210427120017
1+
20210505171935
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20210427120017
1+
20210505171935

sources/net.sf.j2s.java.core/src/javajs/util/CifDataParser.java

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -862,21 +862,35 @@ protected boolean isTerminator(char c) {
862862
protected boolean isQuote(char ch) {
863863
switch (ch) {
864864
case '\'':
865-
case '\"':
865+
case '"':
866866
case '\1':
867+
case '[':
868+
case ']':
867869
return true;
868870
}
869871
return false;
870872
}
871873

872874
/**
873-
* CIF 1.0 only.
875+
* CIF 1.0 only.
874876
*
875-
*
876-
* @param ch current character being pointed to
877+
*
878+
* @param ch
879+
* current character being pointed to
877880
* @return a String data object
878881
*/
879882
protected Object getQuotedStringOrObject(char ch) {
883+
switch (ch) {
884+
case '[':
885+
try {
886+
return readList();
887+
} catch (Exception e) {
888+
System.out.println("exception in CifDataParser ; " + e);
889+
}
890+
case ']':
891+
ich++;
892+
return "]";
893+
}
880894
int ichStart = ich;
881895
char chClosingQuote = ch;
882896
boolean wasQuote = false;
@@ -896,10 +910,49 @@ protected Object getQuotedStringOrObject(char ch) {
896910
pt2++;
897911
} else {
898912
// throw away the last white character
899-
++ich;
913+
++ich;
900914
}
901915
return str.substring(pt1, pt2);
902916
}
903917

918+
/**
919+
* Read a CIF 2.0 list structure, converting it to either a JSON string or to
920+
* a Java data structure
921+
*
922+
* @return a string or data structure, depending upon setting asObject
923+
* @throws Exception
924+
*/
925+
public Object readList() throws Exception {
926+
ich++;
927+
// save the current globals cterm and nullString,
928+
// and restore them afterward.
929+
// nullString is what is returned for '.' and '?';
930+
// for the Jmol CifReader only, this needs to be "\0"
931+
char cterm0 = cterm;
932+
cterm = ']';
933+
String ns = nullString;
934+
nullString = null;
935+
Lst<Object> lst = (asObject ? new Lst<Object>() : null);
936+
int n = 0;
937+
String str = "";
938+
while (true) {
939+
// Iteratively pick up all the objects until the closing bracket
940+
// This is akin to an array "deep copy"
941+
Object value = (asObject ? getNextTokenObject() : getNextToken());
942+
if (value == null || value.equals("]"))
943+
break;
944+
if (asObject) {
945+
lst.addLast(value);
946+
} else {
947+
if (n++ > 0)
948+
str += ",";
949+
str += value;
950+
}
951+
}
952+
cterm = cterm0;
953+
nullString = ns;
954+
return (asObject ? lst : "[" + str + "]");
955+
}
956+
904957

905958
}

sources/net.sf.j2s.java.core/src/javajs/util/DF.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,20 @@ public static String formatDecimalTrimmed(double x, int precision) {
175175
return str.substring(0, m + 1); // 0.##...
176176
}
177177

178+
/**
179+
* an alternative to DecimalFormat "0.0"
180+
*
181+
* @param x
182+
* @param precision
183+
* @return formatted number
184+
*/
185+
public static String formatDecimalTrimmed0(double x, int precision) {
186+
String str = DF.formatDecimalDbl(x, precision);
187+
int m = str.length() - 1;
188+
int pt = str.indexOf(".") + 1;
189+
while (m > pt && str.charAt(m) == '0')
190+
m--;
191+
return str.substring(0, m + 1); // 0.0##...
192+
}
193+
178194
}

0 commit comments

Comments
 (0)