-
Notifications
You must be signed in to change notification settings - Fork 761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add escape for management key #4363
Changes from 5 commits
7bb3f65
2d05699
4f4c3b2
5653309
0cfe168
5a813da
f7d2bff
1de96c6
2ef2e6b
be71026
56124a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// 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. | ||
|
||
use std::string::FromUtf8Error; | ||
|
||
pub fn escape_for_key(key: &str) -> Result<String, FromUtf8Error> { | ||
let mut new_key = Vec::with_capacity(key.len()); | ||
|
||
fn hex(num: u8) -> u8 { | ||
match num { | ||
0..=9 => b'0' + num, | ||
10..=15 => b'a' + (num - 10), | ||
unreachable => unreachable!("Unreachable branch num = {}", unreachable), | ||
} | ||
} | ||
|
||
for char in key.as_bytes() { | ||
match char { | ||
b'0'..=b'9' => new_key.push(*char), | ||
b'_' | b'a'..=b'z' | b'A'..=b'Z' => new_key.push(*char), | ||
_other => { | ||
new_key.push(b'%'); | ||
new_key.push(hex(*char / 16)); | ||
new_key.push(hex(*char % 16)); | ||
} | ||
} | ||
} | ||
|
||
String::from_utf8(new_key) | ||
} | ||
|
||
pub fn unescape_for_key(key: &str) -> Result<String, FromUtf8Error> { | ||
let mut new_key = Vec::with_capacity(key.len()); | ||
|
||
fn unhex(num: u8) -> u8 { | ||
match num { | ||
b'0'..=b'9' => num - b'0', | ||
b'a'..=b'f' => num - b'a', | ||
unreachable => unreachable!("Unreachable branch num = {}", unreachable), | ||
} | ||
} | ||
|
||
let bytes = key.as_bytes(); | ||
|
||
let mut index = 0; | ||
while index < bytes.len() { | ||
match bytes[index] { | ||
b'%' => { | ||
let mut num = unhex(bytes[index + 1]) * 16; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check the index out of range if the index is already the last byte? If we can guarantee There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me I prefer to use iterator here so that we just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This function is only used with reverse case of escape_for_key. I think there is no need to check the input. I have leave a comment in the code.
In this function, the iterator sometimes needs to move 3 steps. Using index would be easier. |
||
num += unhex(bytes[index + 2]); | ||
new_key.push(num); | ||
index += 3; | ||
} | ||
other => { | ||
new_key.push(other); | ||
index += 1; | ||
} | ||
} | ||
} | ||
|
||
String::from_utf8(new_key) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to add some comments about this escape?
Like the raw string is, now we escape to what?
Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also,some unit test cases for this
string_func
module is prefered.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have add comment and unit test.