private IEnumerable <SharpValue> GetVariableRow(string value, bool useCache = true) { var rows = useCache && _cachedRow != null ? _cachedRow : GetColumnValues(); int charpos = 0; for (int pos = 0; pos < _columns.Count && charpos < value.Length; pos++) { var startpos = charpos; bool quoted = false; bool escaped = false; bool hadquotes = false; while (charpos < value.Length && !(!quoted && !escaped && (value[charpos] == Delimiter))) { var c = value[charpos]; charpos++; if (quoted) { if (escaped) { escaped = false; continue; } if (c == '\\') { escaped = true; continue; } if (c == '"') { quoted = false; } continue; } if (c == '"') { quoted = true; hadquotes = true; } } var val = value.Substring(startpos, charpos - startpos); if (hadquotes) { val = EncodedString.ParseQuotedString(val); } rows[pos].Value = val; charpos++; } return(rows); }
public void Parse(string row, string defaultPath = "") { AdditionalInfo.Clear(); QueryPath.Clear(); ValueType = SharpValueType.None; NodeType = SharpNodeType.Any; StringBuilder sbPath = new StringBuilder(); StringBuilder sbType = new StringBuilder(); StringBuilder sbFormat = new StringBuilder(); StringBuilder sbDefaultValue = new StringBuilder(); StringBuilder sbQueryPath = new StringBuilder(); StringBuilder sbQueryValue = new StringBuilder(); StringBuilder sbIndex = new StringBuilder(); bool parseIndex = false; bool parsePath = true; bool parseQueryPath = false; bool parseQueryValue = false; bool parseType = false; bool parseFormat = false; bool parseDefaultValue = false; bool isValueNode = false; bool isAttribute = false; bool isPathExpanded = false; bool isPathLocked = false; int formatCount = 0; int pos = 0; while (pos < row.Length) { char c = row[pos++]; if (char.IsWhiteSpace(c)) { continue; } if (c == '$') { parseIndex = true; } else { pos--; } break; } while (pos < row.Length) { char c = row[pos++]; if (parseIndex) { if (c == '=') { int index = -1; if (int.TryParse(sbIndex.ToString().Trim(), out index)) { Index = index; } parseIndex = false; } else { sbIndex.Append(c); } } else if (parseDefaultValue) { sbDefaultValue.Append(c); } else if (parseFormat) { if (c == '}') { if (formatCount >= 0) { parseFormat = false; parsePath = true; AdditionalInfo["Format"] = new SharpValue(sbFormat.ToString()); } else { formatCount--; } } else { if (c == '{') { formatCount++; } sbFormat.Append(c); } } else if (parseType) { if (c == ' ' || c == '=' || c == '{' || c == ':') { var t = sbType.ToString().Trim(); if (!string.IsNullOrEmpty(t)) { SharpNodeType nt; if (Enum.TryParse(t, true, out nt)) { NodeType = nt; } SharpValueType vt; if (Enum.TryParse(t, true, out vt)) { ValueType = vt; } } sbType.Clear(); if (c == '=' || c == ':') { parseType = false; parsePath = true; pos--; continue; } else if (c == '{') { parseType = false; parseFormat = true; } } else { sbType.Append(c); } } else if (parseQueryValue) { if (c == ']') { QueryPath[sbPath.ToString()] = new KeyValuePair <string, string>(sbQueryPath.ToString(), sbQueryValue.ToString()); parseQueryValue = false; sbQueryPath.Clear(); sbQueryValue.Clear(); } else { sbQueryValue.Append(c); } } else if (parseQueryPath) { if (c == '=') { parseQueryPath = false; parseQueryValue = true; } else if (c == ']') { parseQueryPath = false; sbQueryPath.Clear(); sbQueryValue.Clear(); } else { sbQueryPath.Append(c); } } else if (parsePath) { if (char.IsWhiteSpace(c)) { continue; } if (!isPathExpanded) { if (c == '~' || c == '=' || c == '{' || c == '.' || c == '@') { sbPath.Append(defaultPath); isPathExpanded = true; if (c != '.') { isValueNode = true; isPathLocked = true; sbPath.Append('/'); } else { continue; } } } else if (c == '.' && !isPathLocked) { var parts = sbPath.ToString().Split('/').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToList(); if (parts.Count > 1) { parts = parts.Take(parts.Count - 1).ToList(); } sbPath = new StringBuilder(String.Join("/", parts)); continue; } if (c != '.') { isPathLocked = true; } if (c == '[') { parseQueryPath = true; } else if (c == '~') { parseType = true; parsePath = false; } else if (c == '{') { parseFormat = true; parsePath = false; } else if (c == ':') { if (pos < row.Length && row[pos] == '=') { pos++; parseDefaultValue = true; parsePath = false; } } else if (c == '=') { isValueNode = true; parseDefaultValue = true; parsePath = false; } else { if (c == '@') { isAttribute = true; isValueNode = true; } else if (c == '#') { isValueNode = true; } else if (c == '/') { isValueNode = false; isAttribute = false; } sbPath.Append(c); isPathExpanded = true; } } } if (parseType) { var t = sbType.ToString().Trim(); if (!string.IsNullOrEmpty(t)) { SharpNodeType nt; if (Enum.TryParse(t, true, out nt)) { NodeType = nt; } SharpValueType vt; if (Enum.TryParse(t, true, out vt)) { ValueType = vt; } } parseType = false; } Path = sbPath.ToString(); if (isValueNode && !isAttribute && !Path.EndsWith("/#")) { if (!Path.EndsWith("/")) { Path += "/#"; } else { Path += "#"; } } if (parseDefaultValue) { var defaultValue = sbDefaultValue.ToString().Trim(); if (defaultValue.StartsWith("\"")) { defaultValue = EncodedString.ParseQuotedString(defaultValue); } AdditionalInfo["DefaultValue"] = new SharpValue(defaultValue); } }
public SharpValue(string value, bool parseQuoted = false) { Type = SharpValueType.String; Value = parseQuoted ? EncodedString.ParseQuotedString(value) : value; }