PageRenderTime 2ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/Mono.Cecil.Metadata/StringHeap.cs

http://github.com/jbevain/cecil
C# | 60 lines | 37 code | 14 blank | 9 comment | 9 complexity | b71fa3e64a90c108795f352cc9e2137c MD5 | raw file
 1//
 2// Author:
 3//   Jb Evain (jbevain@gmail.com)
 4//
 5// Copyright (c) 2008 - 2015 Jb Evain
 6// Copyright (c) 2008 - 2011 Novell, Inc.
 7//
 8// Licensed under the MIT/X11 license.
 9//
10
11using System;
12using System.Collections.Generic;
13using System.Text;
14
15namespace Mono.Cecil.Metadata {
16
17	class StringHeap : Heap {
18
19		readonly Dictionary<uint, string> strings = new Dictionary<uint, string> ();
20
21		public StringHeap (byte [] data)
22			: base (data)
23		{
24		}
25
26		public string Read (uint index)
27		{
28			if (index == 0)
29				return string.Empty;
30
31			string @string;
32			if (strings.TryGetValue (index, out @string))
33				return @string;
34
35			if (index > data.Length - 1)
36				return string.Empty;
37
38			@string = ReadStringAt (index);
39			if (@string.Length != 0)
40				strings.Add (index, @string);
41
42			return @string;
43		}
44
45		protected virtual string ReadStringAt (uint index)
46		{
47			int length = 0;
48			int start = (int) index;
49
50			for (int i = start; ; i++) {
51				if (data [i] == 0)
52					break;
53
54				length++;
55			}
56
57			return Encoding.UTF8.GetString (data, start, length);
58		}
59	}
60}