-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChemistryCSVParser.cs
75 lines (67 loc) · 2.14 KB
/
ChemistryCSVParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Text;
using System.Collections.Generic;
namespace ChemTools {
/// <summary>
/// Parses very simple comma separated values in the format int,double,string,string,
/// which corresponds to number, mass, name, symbol
/// </summary>
public class ChemistryCSVParser {
/// <summary>
/// Only method that needs to be used by a program.
/// returns a <c>System.Collections.Generic.List<Element></c> of the outputted elements.
/// </summary>
/// <example>
/// Usage:
/// <c>
/// input = "1,2.00,Hydrogen,H";
/// List<Element> elements = ChemistryCSVParser.ParseString(input);
/// Console.WriteLine(elements.Length); //prints "1"
/// Console.WriteLine(elements[0].symbol); //prints "H"
/// Console.WriteLine(elements[0].masss); //prints "1.00"
/// </c>
/// </example>
public static List<Element> ParseString(string str) {
//make an instance of this class and operate on it in order to be thread-safe
return new ChemistryCSVParser(str).ElementList;
}
//reference to the original input string
private string input;
//the posiion of the next character in the string
private int nextIndex;
private ChemistryCSVParser(string str){
input = str;
nextIndex = 0;
}
//actually does the parsing
//reads the data, creates a new Element, and adds it to the output list
private List<Element> ElementList {
get {
List<Element> elements = new List<Element>();
int number;
double mass;
string name, symbol;
while(nextIndex < input.Length) {
number = int.Parse(ReadValue());
mass = double.Parse(ReadValue());
name = ReadValue();
symbol = ReadValue();
elements.Add(new Element(symbol, name, number, mass));
}
return elements;
}
}
//reads from nextIndex until a comma (,) is reached
//and returns that string (updating nextValue)
//i.e. nextIndex=0, ReadValue on "this,is,a,string" would return "this"
private string ReadValue() {
StringBuilder sb = new StringBuilder();
char c = input[nextIndex++];
while(nextIndex < input.Length && c != ',') {
sb.Append(c);
c = input[nextIndex++];
}
return sb.ToString();
}
}
}