aws-sdk-s3 1.135.0

AWS SDK for Amazon Simple Storage Service
Documentation
// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */

use crate::endpoint_lib::diagnostic::DiagnosticCollector;

/// Splits a string into an array of substrings based on a delimiter.
/// Specification for this function can be found in the [Smithy spec](https://smithy.io/2.0/additional-specs/rules-engine/standard-library.html#split-function).
///
/// ### Arguments
/// * `value` - The input string to split
/// * `delimiter` - The separator (must be non-empty)
/// * `limit` - Controls split behavior:
///   - `0`: Split all occurrences
///   - `1`: No split (returns original string as single element)
///   - `>1`: Split into at most 'limit' parts
///
/// ### Returns
/// `Vec<&str>` containing the split parts
pub(crate) fn split<'a>(value: &'a str, delimiter: &str, limit: usize, _dc: &mut DiagnosticCollector) -> Vec<&'a str> {
    if limit == 0 {
        return value.split(delimiter).collect();
    }

    value.splitn(limit, delimiter).collect()
}

// Test cases sourced from Smithy spec:
// https://smithy.io/2.0/additional-specs/rules-engine/standard-library.html#rules-engine-standard-library-split-examples
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_split_unlimited() {
        assert_eq!(split("a--b--c", "--", 0, &mut DiagnosticCollector::new()), vec!["a", "b", "c"]);
        assert_eq!(
            split("--x-s3--azid--suffix", "--", 0, &mut DiagnosticCollector::new()),
            vec!["", "x-s3", "azid", "suffix"]
        );
    }

    #[test]
    fn test_split_with_limit() {
        assert_eq!(split("a--b--c", "--", 2, &mut DiagnosticCollector::new()), vec!["a", "b--c"]);
        assert_eq!(
            split("--x-s3--azid--suffix", "--", 2, &mut DiagnosticCollector::new()),
            vec!["", "x-s3--azid--suffix"]
        );
    }

    #[test]
    fn test_split_no_split() {
        assert_eq!(split("a--b--c", "--", 1, &mut DiagnosticCollector::new()), vec!["a--b--c"]);
        assert_eq!(split("mybucket", "--", 1, &mut DiagnosticCollector::new()), vec!["mybucket"]);
    }

    #[test]
    fn test_split_empty_string() {
        assert_eq!(split("", "--", 0, &mut DiagnosticCollector::new()), vec![""]);
    }

    #[test]
    fn test_split_delimiter_only() {
        assert_eq!(split("--", "--", 0, &mut DiagnosticCollector::new()), vec!["", ""]);
        assert_eq!(split("----", "--", 0, &mut DiagnosticCollector::new()), vec!["", "", ""]);
    }

    #[test]
    fn test_split_with_empty_parts() {
        assert_eq!(split("--b--", "--", 0, &mut DiagnosticCollector::new()), vec!["", "b", ""]);
    }

    #[test]
    fn test_split_no_delimiter_found() {
        assert_eq!(split("abc", "x", 0, &mut DiagnosticCollector::new()), vec!["abc"]);
    }
}