mysql_common/misc/
mod.rs

1// Copyright (c) 2017 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::io::{self};
10
11pub mod raw;
12
13/// Returns length of length-encoded-integer representation of `x`.
14pub fn lenenc_int_len(x: u64) -> u64 {
15    if x < 251 {
16        1
17    } else if x < 65_536 {
18        3
19    } else if x < 16_777_216 {
20        4
21    } else {
22        9
23    }
24}
25
26/// Returns length of lenght-encoded-string representation of `s`.
27pub fn lenenc_str_len(s: &[u8]) -> u64 {
28    let len = s.len() as u64;
29    lenenc_int_len(len) + len
30}
31
32pub(crate) fn unexpected_buf_eof() -> io::Error {
33    io::Error::new(
34        io::ErrorKind::UnexpectedEof,
35        "can't parse: buf doesn't have enough data",
36    )
37}
38
39/// Splits server 'version' string into three numeric pieces.
40///
41/// It'll return `(0, 0, 0)` in case of error.
42pub fn split_version<T: AsRef<[u8]>>(version_str: T) -> (u8, u8, u8) {
43    let bytes = version_str.as_ref();
44    split_version_inner(bytes).unwrap_or((0, 0, 0))
45}
46
47// Split into its own function for two reasons:
48// 1. Generic function will be instantiated for every type, increasing code size
49// 2. It allows using Option and ? operator without breaking public API
50fn split_version_inner(input: &[u8]) -> Option<(u8, u8, u8)> {
51    let mut nums = [0_u8; 3];
52    let mut iter = input.split(|c| *c == b'.');
53    for (i, chunk) in (&mut iter).take(2).enumerate() {
54        nums[i] = btoi::btoi(chunk).ok()?;
55    }
56    // allow junk at the end of the final part of the version
57    let chunk_with_junk = iter.next()?;
58    let end_of_digits = chunk_with_junk.iter().position(|c| *c < b'0' || *c > b'9');
59    let chunk = match end_of_digits {
60        Some(pos) => &chunk_with_junk[..pos],
61        None => chunk_with_junk,
62    };
63    nums[2] = btoi::btoi(chunk).ok()?;
64
65    Some((nums[0], nums[1], nums[2]))
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn should_split_version() {
74        assert_eq!((1, 2, 3), split_version("1.2.3"));
75        assert_eq!((10, 20, 30), split_version("10.20.30foo"));
76        assert_eq!((0, 0, 0), split_version("100.200.300foo"));
77        assert_eq!((0, 0, 0), split_version("100.200foo"));
78        assert_eq!((0, 0, 0), split_version("1,2.3"));
79        assert_eq!((0, 0, 0), split_version("1"));
80        assert_eq!((0, 0, 0), split_version("1.2"));
81    }
82}