-
Notifications
You must be signed in to change notification settings - Fork 105
/
string.rb
128 lines (116 loc) · 3.3 KB
/
string.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# rubocop:todo all
# -*- coding: utf-8 -*-
# frozen_string_literal: true
# Copyright (C) 2009-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
module BSON
# Injects behaviour for encoding and decoding string values to and from
# raw bytes as specified by the BSON spec.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
module String
# A string is type 0x02 in the BSON spec.
#
# @since 2.0.0
BSON_TYPE = ::String.new(2.chr, encoding: BINARY).freeze
# Regex for matching illegal BSON keys.
#
# @since 4.1.0
ILLEGAL_KEY = /(\A[$])|(\.)/
# Get the string as encoded BSON.
#
# @example Get the string as encoded BSON.
# "test".to_bson
#
# @raise [ EncodingError ] If the string is not UTF-8.
#
# @return [ BSON::ByteBuffer ] The buffer with the encoded object.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def to_bson(buffer = ByteBuffer.new)
buffer.put_string(self)
end
# Get the string as a BSON key name encoded C string with checking for special characters.
#
# @example Get the string as key name.
# "test".to_bson_key
#
# @raise [ EncodingError ] If the string is not UTF-8.
#
# @return [ String ] The encoded string.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def to_bson_key
self
end
# Convert the string to an object id. This will only work for strings of size
# 12.
#
# @example Convert the string to an object id.
# string.to_bson_object_id
#
# @note This is used for repairing legacy bson data.
#
# @raise [ BSON::Error::InvalidObjectId ] If the string is not 12 elements.
#
# @return [ String ] The raw object id bytes.
#
# @since 2.0.0
def to_bson_object_id
ObjectId.repair(self)
end
# Convert the string to a hexidecimal representation.
#
# @example Convert the string to hex.
# "\x01".to_hex_string
#
# @return [ String ] The string as hex.
#
# @since 2.0.0
def to_hex_string
unpack("H*")[0]
end
module ClassMethods
# Deserialize a string from BSON.
#
# @param [ ByteBuffer ] buffer The byte buffer.
#
# @option options [ nil | :bson ] :mode Decoding mode to use.
#
# @return [ Regexp ] The decoded string.
#
# @see http://bsonspec.org/#/specification
#
# @since 2.0.0
def from_bson(buffer, **options)
buffer.get_string
end
end
# Register this type when the module is loaded.
#
# @since 2.0.0
Registry.register(BSON_TYPE, ::String)
end
# Enrich the core String class with this module.
#
# @since 2.0.0
::String.send(:include, String)
::String.send(:extend, String::ClassMethods)
end