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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![allow(unknown_lints)]
#![allow(clippy::manual_let_else)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(missing_copy_implementations)]
#![warn(rust_2018_idioms)]
#![warn(rust_2021_compatibility)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![forbid(unsafe_code)]
// Enable feature callouts in generated documentation:
// https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html
//
// This approach is borrowed from tokio.
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_alias))]

//! Functions for working with Ruby containers that respond to `#[]` or "aref".
//!
//! # Examples
//!
//! Index into arrays:
//!
//! ```
//! # fn example() -> Option<()> {
//! let data = [1, 2, 3, 4, 5];
//!
//! // Positive offset
//! let offset = 2;
//! let index = scolapasta_aref::offset_to_index(offset, data.len())?;
//! assert_eq!(index, 2);
//! assert_eq!(data[index], 3);
//!
//! // Negative offset
//! let offset = -3;
//! let index = scolapasta_aref::offset_to_index(offset, data.len())?;
//! assert_eq!(index, 2);
//! assert_eq!(data[index], 3);
//!
//! // Out-of-bounds offset
//! let offset = 10;
//! let index = scolapasta_aref::offset_to_index(offset, data.len())?;
//! assert_eq!(index, 10);
//!
//! // Out-of-bounds negative offset
//! let offset = -10;
//! let index = scolapasta_aref::offset_to_index(offset, data.len());
//! assert_eq!(index, None);
//! # Some(())
//! # }
//! # example().unwrap()
//! ```
//!
//! Index into strings:
//!
//! ```
//! # fn example() -> Option<()> {
//! let data = "Hello, World!";
//!
//! // Positive offset
//! let offset = 7;
//! let index = scolapasta_aref::offset_to_index(offset, data.len())?;
//! assert_eq!(index, 7);
//! assert_eq!(&data[index..], "World!");
//!
//! // Negative offset
//! let offset = -6;
//! let index = scolapasta_aref::offset_to_index(offset, data.len())?;
//! assert_eq!(index, 7);
//! assert_eq!(&data[index..], "World!");
//!
//! // Out-of-bounds offset
//! let offset = 20;
//! let index = scolapasta_aref::offset_to_index(offset, data.len())?;
//! assert_eq!(index, 20);
//!
//! // Out-of-bounds negative offset
//! let offset = -20;
//! let index = scolapasta_aref::offset_to_index(offset, data.len());
//! assert_eq!(index, None);
//! # Some(())
//! # }
//! # example().unwrap()
//! ```

#![no_std]

// Ensure code blocks in `README.md` compile
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme {}

/// Convert a signed aref offset to a `usize` index into the underlying container.
///
/// Negative indexes are interpreted as indexing from the end of the container
/// as long as their magnitude is less than the given length.
///
/// Callers must still check whether the returned index is in bounds for the
/// container. The returned index may be out of range since this routine can be
/// used to calculate indexes beyond the length of the container during
/// assignment (for example, `Array#[]=` may perform length-extension upon an
/// out-of-bounds index).
///
/// # Examples
///
/// ```
/// # fn example() -> Option<()> {
/// let data = "ABC, 123, XYZ";
///
/// let offset = 6;
/// let index = scolapasta_aref::offset_to_index(offset, data.len())?;
/// assert_eq!(index, 6);
/// assert_eq!(&data[index..], "23, XYZ");
///
/// let offset = 55;
/// let index = scolapasta_aref::offset_to_index(offset, data.len())?;
/// assert_eq!(index, 55);
///
/// let offset = -5;
/// let index = scolapasta_aref::offset_to_index(offset, data.len())?;
/// assert_eq!(index, 8);
/// assert_eq!(&data[index..], ", XYZ");
///
/// let offset = -44;
/// let index = scolapasta_aref::offset_to_index(offset, data.len());
/// assert_eq!(index, None);
/// # Some(())
/// # }
/// # example().unwrap()
/// ```
#[must_use]
pub fn offset_to_index(index: i64, len: usize) -> Option<usize> {
    // Here's an example of this behavior from `String`. All containers that
    // respond to `#[]` ("aref") behave similarly.
    //
    // ```
    // [3.0.1] > s = "abc"
    // => "abc"
    //
    // [3.0.1] > s[-2]
    // => "b"
    // [3.0.1] > s[-3]
    // => "a"
    // [3.0.1] > s[-4]
    // => nil
    //
    // [3.0.1] > s[-2, 10]
    // => "bc"
    // [3.0.1] > s[-3, 10]
    // => "abc"
    // [3.0.1] > s[-4, 10]
    //
    // [3.0.2] > s.byteslice(-2, 10)
    // => "bc"
    // [3.0.2] > s.byteslice(-3, 10)
    // => "abc"
    // [3.0.2] > s.byteslice(-4, 10)
    // => nil
    // => nil
    // ```
    match usize::try_from(index) {
        Ok(index) => Some(index),
        Err(_) => index
            .checked_neg()
            .and_then(|index| usize::try_from(index).ok())
            .and_then(|index| len.checked_sub(index)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_zero_index() {
        // Test case: index = 0, len = 0
        assert_eq!(offset_to_index(0_i64, 0_usize), Some(0_usize));

        // Test case: index = 0, len = 1
        assert_eq!(offset_to_index(0_i64, 1_usize), Some(0_usize));

        // Test case: index = 0, len = 10
        assert_eq!(offset_to_index(0, 10), Some(0_usize));

        // Test case: index = 0, len = usize::MAX
        assert_eq!(offset_to_index(0_i64, usize::MAX), Some(0_usize));
    }

    #[test]
    fn test_positive_index() {
        // Test case: index = 1, len = 0
        assert_eq!(offset_to_index(1_i64, 0_usize), Some(1_usize));

        // Test case: index = 1, len = 1
        assert_eq!(offset_to_index(1_i64, 1_usize), Some(1_usize));

        // Test case: index = 1, len = 2
        assert_eq!(offset_to_index(1_i64, 2_usize), Some(1_usize));

        // Test case: index = 1, len = usize::MAX
        assert_eq!(offset_to_index(1_i64, usize::MAX), Some(1_usize));

        // Test case: index = 15, len = 10
        assert_eq!(offset_to_index(15, 10), Some(15_usize));

        // Test case: index = 123, len = 0
        assert_eq!(offset_to_index(123_i64, 0_usize), Some(123_usize));

        // Test case: index = 123, len = 1
        assert_eq!(offset_to_index(123_i64, 1_usize), Some(123_usize));

        // Test case: index = 123, len = 123
        assert_eq!(offset_to_index(123_i64, 123_usize), Some(123_usize));

        // Test case: index = 123, len = 123
        assert_eq!(offset_to_index(123_i64, 124_usize), Some(123_usize));

        // Test case: index = 123, len = 123
        assert_eq!(offset_to_index(123_i64, 500_usize), Some(123_usize));

        // Test case: index = 123, len = usize::MAX
        assert_eq!(offset_to_index(123_i64, usize::MAX), Some(123_usize));

        // Test case: index = i64::MAX, len = 5
        #[cfg(target_pointer_width = "64")]
        assert_eq!(offset_to_index(i64::MAX, 5), Some(usize::try_from(i64::MAX).unwrap()));

        // Test case: index = i64::MAX, len = usize::MAX
        #[cfg(target_pointer_width = "64")]
        assert_eq!(
            offset_to_index(i64::MAX, usize::MAX),
            Some(usize::try_from(i64::MAX).unwrap())
        );

        // Test case: index = 100, len = 1000
        assert_eq!(offset_to_index(100_i64, 1000_usize), Some(100_usize));

        // Test case: index = 500, len = 500
        assert_eq!(offset_to_index(500_i64, 500_usize), Some(500_usize));

        // Test case: index = 999, len = 100
        assert_eq!(offset_to_index(999_i64, 100_usize), Some(999_usize));
    }

    #[test]
    fn test_negative_index() {
        // Test case: index = -1, len = 0
        assert_eq!(offset_to_index(-1_i64, 0_usize), None);

        // Test case: index = -1, len = 1
        assert_eq!(offset_to_index(-1_i64, 1_usize), Some(0_usize));

        // Test case: index = -1, len = 2
        assert_eq!(offset_to_index(-1_i64, 2_usize), Some(1_usize));

        // Test case: index = -1, len = 10
        assert_eq!(offset_to_index(-1_i64, 10_usize), Some(9_usize));

        // Test case: index = -1, len = 245
        assert_eq!(offset_to_index(-1_i64, 245_usize), Some(244_usize));

        // Test case: index = -10, len = 0
        assert_eq!(offset_to_index(-10_i64, 0_usize), None);

        // Test case: index = -10, len = 1
        assert_eq!(offset_to_index(-10_i64, 1_usize), None);

        // Test case: index = -10, len = 2
        assert_eq!(offset_to_index(-10_i64, 2_usize), None);

        // Test case: index = -10, len = 10
        assert_eq!(offset_to_index(-10_i64, 10_usize), Some(0_usize));

        // Test case: index = -10, len = 245
        assert_eq!(offset_to_index(-10_i64, 245_usize), Some(235_usize));

        // Test case: index = -123, len = 0
        assert_eq!(offset_to_index(-123_i64, 0_usize), None);

        // Test case: index = -123, len = 1
        assert_eq!(offset_to_index(-123_i64, 1_usize), None);

        // Test case: index = -123, len = 2
        assert_eq!(offset_to_index(-123_i64, 2_usize), None);

        // Test case: index = -123, len = 10
        assert_eq!(offset_to_index(-123_i64, 10_usize), None);

        // Test case: index = -123, len = 245
        assert_eq!(offset_to_index(-123_i64, 245_usize), Some(122_usize));

        // Test case: index = i64::MIN, len = 0
        assert_eq!(offset_to_index(i64::MIN, 0_usize), None);

        // Test case: index = i64::MIN, len = 1
        assert_eq!(offset_to_index(i64::MIN, 1_usize), None);

        // Test case: index = i64::MIN, len = 2
        assert_eq!(offset_to_index(i64::MIN, 2_usize), None);

        // Test case: index = i64::MIN, len = 10
        assert_eq!(offset_to_index(i64::MIN, 10_usize), None);

        // Test case: index = i64::MIN, len = 245
        assert_eq!(offset_to_index(i64::MIN, 245_usize), None);
    }

    #[test]
    fn test_out_of_bounds_positive_offset() {
        // Test case: Offset greater than or equal to length
        //
        // ```
        // [3.2.2] > a = [1,2,3,4,5]
        // => [1, 2, 3, 4, 5]
        // [3.2.2] > a[10]
        // => nil
        // [3.2.2] > a[10] = 'a'
        // => "a"
        // [3.2.2] > a
        // => [1, 2, 3, 4, 5, nil, nil, nil, nil, nil, "a"]
        // ```
        assert_eq!(offset_to_index(10, 5), Some(10_usize));
    }

    #[test]
    fn test_positive_offset_equal_to_length() {
        // ```
        // [3.2.2] > a = [1,2,3,4,5]
        // => [1, 2, 3, 4, 5]
        // [3.2.2] > a[5]
        // => nil
        // [3.2.2] > a[5, 0]
        // => []
        // [3.2.2] > a[5] = 'a'
        // => "a"
        // [3.2.2] > a
        // => [1, 2, 3, 4, 5, "a"]
        // ```
        assert_eq!(offset_to_index(5, 5), Some(5_usize));
    }

    #[test]
    fn test_negative_offset_of_magnitude_length() {
        // Test case: Offset equal to negative length
        //
        // ```
        // [3.2.2] > a = [1,2,3,4,5]
        // => [1, 2, 3, 4, 5]
        // [3.2.2] > a[-5]
        // => 1
        // [3.2.2] > a[-5] = 'a'
        // => "a"
        // [3.2.2] > a
        // => ["a", 2, 3, 4, 5]
        // ```
        assert_eq!(offset_to_index(-5, 5), Some(0));

        assert_eq!(offset_to_index(-10, 10), Some(0_usize));
    }

    #[test]
    fn test_invalid_negative_offset() {
        // Test case: Offset less than negative length
        //
        // ```
        // [3.2.2] > a = [1,2,3,4,5]
        // => [1, 2, 3, 4, 5]
        // [3.2.2] > a[-10]
        // => nil
        // [3.2.2] > a[-10] = 'a'
        // (irb):5:in `<main>': index -10 too small for array; minimum: -5 (IndexError)
        // ```
        assert_eq!(offset_to_index(-10, 5), None);
    }

    #[test]
    fn test_edge_cases() {
        // Test case: Length is zero
        assert_eq!(offset_to_index(0, 0), Some(0_usize));

        // Test case: Offset is the minimum `i64` value
        assert_eq!(offset_to_index(i64::MIN, 10), None);

        // Test case: Offset is the maximum `i64` value
        #[cfg(target_pointer_width = "64")]
        assert_eq!(offset_to_index(i64::MAX, 10), Some(usize::try_from(i64::MAX).unwrap()));

        // Test case: index = 0, len = usize::MAX
        assert_eq!(offset_to_index(0_i64, usize::MAX), Some(0_usize));

        // Test case: index = 1, len = usize::MAX
        assert_eq!(offset_to_index(1_i64, usize::MAX), Some(1_usize));

        // Test case: index = -1, len = usize::MAX
        assert_eq!(offset_to_index(-1_i64, usize::MAX), Some(usize::MAX - 1));

        // Test case: index = 10, len = usize::MAX
        assert_eq!(offset_to_index(10, usize::MAX), Some(10_usize));

        // Test case: index = i64::MAX, len = usize::MAX
        #[cfg(target_pointer_width = "64")]
        assert_eq!(
            offset_to_index(i64::MAX, usize::MAX),
            Some(usize::try_from(i64::MAX).unwrap())
        );
    }
}