1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 module capnproto.Text;
23 
24 import java.nio.ByteBuffer;
25 
26 import capnproto.SegmentBuilder;
27 import capnproto.SegmentReader;
28 import capnproto.WireHelpers;
29 
30 struct Text
31 {
32 public: //Types.
33 	static struct Reader
34 	{
35 	public: //Variables.
36 		ByteBuffer buffer;
37 		int offset; //In bytes.
38 		size_t size; //In bytes, not including NUL terminator.
39 	
40 	public: //Methods.
41 		this(ByteBuffer buffer, int offset, int size)
42 		{
43 			this.buffer = buffer;
44 			this.offset = offset * 8;
45 			this.size = size;
46 		}
47 		
48 		this(string value)
49 		{
50 			this.buffer = ByteBuffer(cast(ubyte[])value);
51 			this.offset = 0;
52 			this.size = value.length;
53 		}
54 		
55 		size_t length()
56 		{
57 			return this.size;
58 		}
59 		
60 		ByteBuffer asByteBuffer()
61 		{
62 			auto dup = this.buffer;
63 			dup.position = this.offset;
64 			auto result = dup.slice();
65 			result.limit = this.size;
66 			return result;
67 		}
68 		
69 		string toString()
70 		{
71 			return cast(string)this.buffer[offset..offset+size];
72 		}
73 	}
74 
75 	static struct Builder
76 	{
77 	public: //Variables.
78 		ByteBuffer buffer;
79 		int offset; //In bytes.
80 		int size; //In bytes.
81 	
82 	public: //Methods.
83 		this(ByteBuffer buffer, int offset, int size)
84 		{
85 			this.buffer = buffer;
86 			this.offset = offset;
87 			this.size = size;
88 		}
89 		
90 		ByteBuffer asByteBuffer()
91 		{
92 			auto dup = this.buffer;
93 			dup.position = this.offset;
94 			auto result = dup.slice();
95 			result.limit = this.size;
96 			return result;
97 		}
98 		
99 		size_t length()
100 		{
101 			return this.size;
102 		}
103 		
104 		string toString()
105 		{
106 			return cast(string)this.buffer[offset..offset+size];
107 		}
108 	}
109 }