| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Data structure storing an association between URI and file contents. |
| 6 /// |
| 7 /// Each URI is also associated with a unique arbitrary path ending in ".dart". |
| 8 /// This allows interfacing with analyzer code that expects to manipulate paths |
| 9 /// rather than URIs. |
| 10 class FileRepository { |
| 11 /// Regular expression matching the arbitrary file paths generated by |
| 12 /// [_pathForIndex]. |
| 13 static final _pathRegexp = new RegExp(r'^/[0-9]+\.dart$'); |
| 14 |
| 15 /// The URIs currently stored in the repository. |
| 16 final _uris = <Uri>[]; |
| 17 |
| 18 /// Map from a URI to its index in [_uris]. |
| 19 final _indexForUri = <Uri, int>{}; |
| 20 |
| 21 /// The file contents associated with the URIs in [_uris]. |
| 22 final _contents = <String>[]; |
| 23 |
| 24 /// Return the contents of the file whose arbitary path is [path]. |
| 25 /// |
| 26 /// The path must have been returned by a previous call to [store] or |
| 27 /// [pathForUri]. |
| 28 String contentsForPath(String path) => _contents[_indexForPath(path)]; |
| 29 |
| 30 /// Return the arbitrary path associated with [uri]. |
| 31 /// |
| 32 /// The uri must have previously been passed to [store]. |
| 33 String pathForUri(Uri uri) { |
| 34 int index = _indexForUri[uri]; |
| 35 assert(index != null); |
| 36 return _pathForIndex(index); |
| 37 } |
| 38 |
| 39 /// Associate the given [uri] with file [contents]. |
| 40 /// |
| 41 /// The arbitrary path associated with the file is returned. |
| 42 String store(Uri uri, String contents) { |
| 43 int index = _indexForUri[uri]; |
| 44 if (index == null) { |
| 45 index = _uris.length; |
| 46 _uris.add(uri); |
| 47 _indexForUri[uri] = index; |
| 48 _contents.add(contents); |
| 49 } else { |
| 50 _contents[index] = contents; |
| 51 } |
| 52 return _pathForIndex(index); |
| 53 } |
| 54 |
| 55 /// Return the URI for the file whose arbitrary path is [path]. |
| 56 /// |
| 57 /// The path must have been returned by a previous call to [store] or |
| 58 /// [pathForUri]. |
| 59 Uri uriForPath(String path) => _uris[_indexForPath(path)]; |
| 60 |
| 61 /// Return the index into [_uris] and [_contents] matching the arbitrary path |
| 62 /// [path]. |
| 63 int _indexForPath(String path) { |
| 64 assert(_pathRegexp.hasMatch(path)); |
| 65 return int.parse(path.substring(1, path.length - 5)); |
| 66 } |
| 67 |
| 68 /// Return the arbitrary path associated with the given index. |
| 69 String _pathForIndex(int index) => '/$index.dart'; |
| 70 } |
| OLD | NEW |