INI Lookup

easy parsing strings hashing 1000 ms · 256 MB

INI Lookup

A legacy service keeps its settings in an INI file: a plain text file split into named sections, each holding key = value settings. Your job is to read the file the way the service does and answer a list of lookups.

File format

The file consists of n lines. Every line contains only printable ASCII characters (codes 32 to 126) and tabs. Below, whitespace means spaces and tabs, and to trim a string means to remove all whitespace from its beginning and its end.

A section name is a string of 1 to 40 characters made of letters (A–Z, a–z), digits, underscores _, hyphens - and spaces, which does not start or end with a space. A key name is the same, except that it may also contain dots .. Neither may contain tabs. For example Data Base and db-1 are section names, and server.port and max size are key names.

Each line has exactly one of the following forms:

  • A blank line: empty, or whitespace only.
  • A comment line: optional whitespace, then ; or #, then any text up to the end of the line. It is ignored.
  • A section header: optional whitespace, [, optional whitespace, a section name, optional whitespace, ], optional whitespace. All lines after it, up to the next section header, belong to that section.
  • A setting line: optional whitespace, a key name, optional whitespace, =, then the raw value: any text up to the end of the line (possibly empty). The value of the setting is the raw value, trimmed. The value may contain any characters, including =, ;, #, [ and ]; they are part of the value and never start a comment.

Setting lines before the first section header belong to the global section, whose name is the empty string.

Section names and key names are case-insensitive: [Server] and [SERVER] denote the same section, and Port and port the same key. Spaces inside names are significant: max size and maxsize are different keys. A section may have several headers in the file; its settings are gathered from all of them. If the same key is set several times in the same section, the last setting in the file wins.

Queries

Each query has the form S.K: a possibly empty string S (the section), a dot, and a key name K. The query string is split at its first dot: S is everything before it and contains no dots, while K may contain further dots. When S is empty, the query asks about the global section. S is either empty or a section name; it, too, is matched case-insensitively and need not appear in the file.

Input

The first line contains two integers n and q (1 ≤ n ≤ 10^5, 1 ≤ q ≤ 10^5), separated by a single space.

The next n lines are the lines of the file, each well formed as described above and at most 200 characters long.

The next q lines contain one query each, with no leading or trailing whitespace.

Every line of the input is terminated by a line feed, except that the last line may or may not have one. There are no carriage returns. The whole input is at most 10^7 characters long.

Output

For each query, in order, print one line. If the section S has a setting for the key K, print its value enclosed in double quotes "; the value is printed exactly as it appears in the file, with its original letter case. Otherwise print NOT FOUND.

Notes

In the first example, name is a global setting, and Server.PORT finds port in section server regardless of case. The key host is set twice in server, and the later value 10.0.0.2 wins. The key name is set only in the global section, not in section server, and section cache does not exist.

In the second example (some of its lines contain tabs), the query db.conn.timeout asks for the key conn.timeout in section db, which is set twice: the later setting, under the second header of db, wins. The value of url is a=b;c # not a comment, because ; and # inside a value are kept. The key empty exists with an empty value, which is printed as "". In section Data Base, max size is overridden by the later MAX SIZE. The last query asks for the key base.max size in section data, which does not exist.

Example 1

Input

10 7
; service configuration
name = demo service

[server]
host = 10.0.0.1
port = 8080
# moved to a new box
host = 10.0.0.2
[client]
retries = 3
.name
Server.PORT
server.host
server.name
CLIENT.retries
client.timeout
cache.size

Output

"demo service"
"8080"
"10.0.0.2"
NOT FOUND
"3"
NOT FOUND
NOT FOUND

Example 2

Input

11 8
	[ db ]
conn.timeout   =   30
url = a=b;c # not a comment
Empty =
[Data Base]
max size = 10
[ db]
	CONN.TIMEOUT	=	45
[ data base ]
MAX SIZE = 20
maxsize = 1
db.conn.timeout
DB.URL
db.empty
data base.max size
Data Base.MaxSize
db.conn
.url
data.base.max size

Output

"45"
"a=b;c # not a comment"
""
"20"
"1"
NOT FOUND
NOT FOUND
NOT FOUND

Hints · reveal one at a time if you're stuck

Hint 1

Most of the work is careful parsing. Decide what kind of line you have from its first non-blank character, and remember that ; and # only start a comment at the beginning of a line, never inside a value.

Hint 2

Store each setting in a map keyed by the pair (section, key), with both names lower-cased and later lines overwriting earlier ones. For a query, split at the first dot only, keep values in their original case, and remember that an empty value still counts as found.

report an issue Prefer your own editor? sooks prep 33 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.