Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensions to Matpower Format Parsing #53

Merged
merged 13 commits into from
Dec 8, 2016
Merged
Prev Previous commit
cleaning up quoted string parsing
ccoffrin committed Dec 7, 2016
commit f1a1fb0e80c9305081161cc9c49e8f4648863e3c
48 changes: 18 additions & 30 deletions src/io/matpower.jl
Original file line number Diff line number Diff line change
@@ -94,51 +94,39 @@ function parse_matlab_data(lines, index, start_char, end_char)
return matrix_dict
end


single_quote_expr = r"\'((\\.|[^\'])*?)\'"

function split_line(mp_line)
if contains(mp_line, "'")
# splits a string on white space escaping text quoted with "'"
if ismatch(single_quote_expr, mp_line)
# splits a string on white space while escaping text quoted with "'"
# note that quotes will be stripped later, when data typing occurs
# TODO is this really the best we can do here??? gonna be super slow

#println(mp_line)
tokens = []
while length(mp_line) > 0 && ismatch(single_quote_expr, mp_line)
#println(mp_line)
m = match(single_quote_expr, mp_line)

buffer = ""
open = false
prev = "none"
for c in mp_line
if c == '\'' && prev != '\\'
if !open
open = true
if length(strip(buffer)) > 0
push!(tokens, buffer)
end
buffer = ""
buffer = string(buffer, c)
else
open = false
buffer = string(buffer, c)
if length(strip(buffer)) > 0
push!(tokens, buffer)
end
buffer = ""
end
else
buffer = string(buffer, c)
if m.offset > 1
push!(tokens, mp_line[1:m.offset-1])
end
prev = c
end
push!(tokens, buffer)
push!(tokens, replace(m.match, "\\'", "'")) # replace escaped quotes

mp_line = mp_line[m.offset+length(m.match):end]
end
if length(mp_line) > 0
push!(tokens, mp_line)
end
#println(tokens)

items = []
for token in tokens
if contains(token, "'")
push!(items, token)
push!(items, strip(token))
else
for parts in split(token)
push!(items, parts)
push!(items, strip(parts))
end
end
end
10 changes: 5 additions & 5 deletions test/data/case3.m
Original file line number Diff line number Diff line change
@@ -64,14 +64,14 @@
% adding extra cell values

mpc.areas_cells = {
'Area 1' 123 'Slack Bus 1' 1.23;
'Area 2' 456 'Slack Bus 3' 4.56;
'Area 1' 123 987 'Slack \'Bus\' 1' 1.23 ;
'Area 2' 456 987 'Slack Bus 3' 4.56 ;
};

%column_names% area_name area refbus_name refbus
%column_names% area_name area area2 refbus_name refbus
mpc.areas_named_cells = {
'Area 1' 123 'Slack Bus 1' 1.23;
'Area 2' 456 'Slack Bus 3' 4.56;
'Area 1' 123 987 'Slack Bus 1' 1.23;
'Area 2' 456 987 'Slack Bus 3' 4.56;
};

%column_names% name number_id
10 changes: 6 additions & 4 deletions test/matpower.jl
Original file line number Diff line number Diff line change
@@ -97,12 +97,12 @@ end
@test haskey(data, "areas_cells")
@test data["areas_cells"][1]["col_1"] == "Area 1"
@test data["areas_cells"][1]["col_2"] == 123
@test data["areas_cells"][1]["col_3"] == "Slack Bus 1"
@test data["areas_cells"][1]["col_4"] == 1.23
@test data["areas_cells"][1]["col_4"] == "Slack 'Bus' 1"
@test data["areas_cells"][1]["col_5"] == 1.23
@test data["areas_cells"][2]["col_1"] == "Area 2"
@test data["areas_cells"][2]["col_2"] == 456
@test data["areas_cells"][2]["col_3"] == "Slack Bus 3"
@test data["areas_cells"][2]["col_4"] == 4.56
@test data["areas_cells"][2]["col_4"] == "Slack Bus 3"
@test data["areas_cells"][2]["col_5"] == 4.56
end

@testset "3-bus extended named matrix from cell" begin
@@ -111,10 +111,12 @@ end
@test haskey(data, "areas_named_cells")
@test data["areas_named_cells"][1]["area_name"] == "Area 1"
@test data["areas_named_cells"][1]["area"] == 123
@test data["areas_named_cells"][1]["area2"] == 987
@test data["areas_named_cells"][1]["refbus_name"] == "Slack Bus 1"
@test data["areas_named_cells"][1]["refbus"] == 1.23
@test data["areas_named_cells"][2]["area_name"] == "Area 2"
@test data["areas_named_cells"][2]["area"] == 456
@test data["areas_named_cells"][2]["area2"] == 987
@test data["areas_named_cells"][2]["refbus_name"] == "Slack Bus 3"
@test data["areas_named_cells"][2]["refbus"] == 4.56
end