Skip to content

Commit 1ac407e

Browse files
authored
[FileFormats.MPS] allow spaces in NAME row (#2146)
1 parent 0adae07 commit 1ac407e

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/FileFormats/MPS/MPS.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ function Base.read!(io::IO, model::Model)
11341134
# TODO: split into hard fields based on column indices.
11351135
items = line_to_items(line)
11361136
if header == HEADER_NAME
1137-
parse_name_line(data, items)
1137+
parse_name_line(data, line)
11381138
elseif header == HEADER_OBJSENSE
11391139
@assert length(items) == 1
11401140
sense = uppercase(items[1])
@@ -1314,15 +1314,12 @@ end
13141314
# NAME
13151315
# ==============================================================================
13161316

1317-
function parse_name_line(data::TempMPSModel, items::Vector{String})
1318-
if !(1 <= length(items) <= 2) || uppercase(items[1]) != "NAME"
1319-
error("Malformed NAME line: $(join(items, " "))")
1320-
end
1321-
if length(items) == 2
1322-
data.name = items[2]
1323-
elseif length(items) == 1
1324-
data.name = ""
1317+
function parse_name_line(data::TempMPSModel, line::String)
1318+
m = match(r"^\s*NAME(.*)"i, line)
1319+
if m === nothing
1320+
error("Malformed NAME line: ", line)
13251321
end
1322+
data.name = strip(m[1])
13261323
return
13271324
end
13281325

test/FileFormats/MPS/MPS.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,35 @@ function test_objsense_next_line()
10361036
return
10371037
end
10381038

1039+
function test_parse_name_line()
1040+
data = MPS.TempMPSModel()
1041+
for (line, name) in (
1042+
"NAME" => "",
1043+
"NAME " => "",
1044+
"NAME \n" => "",
1045+
"NAmE" => "",
1046+
"NaME " => "",
1047+
"name \n" => "",
1048+
"name abc" => "abc",
1049+
"NAME PILOTNOV (PILOTS) INTEGRATED MODEL -- NOVEMBER 1979" => "PILOTNOV (PILOTS) INTEGRATED MODEL -- NOVEMBER 1979",
1050+
"Na d" => nothing,
1051+
"the name" => nothing,
1052+
" NAME" => "",
1053+
" NAME foo" => "foo",
1054+
"" => nothing,
1055+
)
1056+
data.name = "_"
1057+
if name === nothing
1058+
err = ErrorException("Malformed NAME line: $line")
1059+
@test_throws err MPS.parse_name_line(data, line)
1060+
else
1061+
MPS.parse_name_line(data, line)
1062+
@test data.name == name
1063+
end
1064+
end
1065+
return
1066+
end
1067+
10391068
function runtests()
10401069
for name in names(@__MODULE__, all = true)
10411070
if startswith("$(name)", "test_")

test/FileFormats/MPS/failing_models/name_spaces.mps

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)