Function rustrict::width_str

source ·
pub fn width_str(s: &str) -> usize
Expand description

Convenience method for getting the width, in m’s, of an entire string.

Warning: If the width overflows, the result is undefined (e.g. panic or overflow).

Examples found in repository?
src/context.rs (line 360)
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
    pub fn process_with_options(
        &mut self,
        message: String,
        options: &ContextProcessingOptions,
    ) -> Result<String, BlockReason> {
        let now = Instant::now();
        let elapsed = self.last_message.map(|l| now - l).unwrap_or(Duration::ZERO);

        let suspicion = self.suspicion.max(1).saturating_mul(self.reports.max(1));

        // How convinced are we that the user is a bad actor.
        let is_kinda_sus = suspicion >= 2;
        let is_impostor = suspicion >= 15;

        // Don't give bad actors the benefit of the doubt when it comes to meanness.
        let meanness_threshold = if is_impostor {
            Type::MILD_OR_HIGHER
        } else if is_kinda_sus {
            Type::MODERATE_OR_HIGHER
        } else {
            Type::SEVERE
        };

        let censor_threshold =
            Type::PROFANE | Type::OFFENSIVE | Type::SEXUAL | (Type::MEAN & meanness_threshold);

        // Don't give bad actors the benefit of letting their first character through.
        let censor_first_character_threshold = if is_kinda_sus {
            censor_threshold
        } else {
            // Mainly for protection against the n-word being discernible.
            Type::OFFENSIVE & Type::SEVERE
        };

        let (mut censored, analysis) = Censor::from_str(&message)
            .with_censor_threshold(censor_threshold)
            .with_censor_first_character_threshold(censor_first_character_threshold)
            .censor_and_analyze();

        let mut censored_str = if should_skip_censor(&message) {
            message.as_str()
        } else {
            censored.as_str()
        };

        if let Some(character_limit) = options.character_limit {
            #[cfg(feature = "width")]
            {
                censored_str = crate::trim_to_width(censored_str, character_limit.get());
            }
            if let Some((limit, _)) = censored_str.char_indices().nth(character_limit.get()) {
                censored_str = &censored_str[..limit];
            }
        }

        if options.trim_whitespace {
            censored_str = trim_whitespace(censored_str);
        }

        if censored_str.len() < censored.len() {
            // Something was trimmed, must must re-allocate.
            censored = String::from(censored_str);
        }

        self.total = self.total.saturating_add(1);
        if analysis.is(Type::INAPPROPRIATE) {
            self.total_inappropriate = self.total_inappropriate.saturating_add(1);
        }

        // Collecting suspicion.
        let type_to_sus = |typ: Type| -> u8 {
            let combined = analysis & typ;
            if combined.is(Type::SEVERE) {
                3
            } else if combined.is(Type::MODERATE) {
                2
            } else if combined.is(Type::MILD) {
                1
            } else {
                0
            }
        };

        // Repetition detection.
        let mut recent_similar = 0;

        if let Some(opts) = options.repetition_limit.as_ref() {
            self.history.retain(|&(_, t)| now - t < opts.memory);

            for (recent_message, _) in &self.history {
                if strsim::normalized_levenshtein(recent_message, &message)
                    >= opts.similarity_threshold as f64
                {
                    recent_similar += 1;
                }
            }
        }

        let mut new_suspicion = type_to_sus(Type::PROFANE | Type::OFFENSIVE | Type::SEXUAL)
            + type_to_sus(Type::EVASIVE)
            + type_to_sus(Type::SPAM);

        if recent_similar >= 2 {
            // Don't penalize as much for repeated messages, since an innocent user may repeat their
            // message multiple times if it was erroneously detected.
            new_suspicion /= 2;
        }

        if ((is_kinda_sus && new_suspicion >= 4) || (is_impostor && new_suspicion >= 2))
            && !options.max_safe_timeout.is_zero()
        {
            if let Some(only_safe_until) =
                self.only_safe_until
                    .unwrap_or(now)
                    .checked_add(if self.reports > 0 {
                        Duration::from_secs(10 * 60)
                    } else {
                        Duration::from_secs(5 * 60)
                    })
            {
                self.only_safe_until = Some(only_safe_until.min(now + options.max_safe_timeout));
            }
        }

        self.suspicion = self.suspicion.saturating_add(new_suspicion);

        let remaining_rate_limit = Self::remaining_duration(&mut self.rate_limited_until, now);

        if let Some(remaining) = options
            .safe_mode_until
            .filter(|_| analysis.isnt(Type::SAFE))
            .and_then(|until| until.checked_duration_since(now))
        {
            Err(BlockReason::Unsafe {
                remaining,
                targeted: false,
            })
        } else if let Some(dur) =
            Self::remaining_duration(&mut self.muted_until, now).filter(|_| options.block_if_muted)
        {
            Err(BlockReason::Muted(dur))
        } else if options.block_if_empty && censored.is_empty() {
            Err(BlockReason::Empty)
        } else if let Some(dur) = options
            .rate_limit
            .as_ref()
            .and_then(|opt| remaining_rate_limit.filter(|_| self.burst_used >= opt.burst))
        {
            Err(BlockReason::Spam(dur))
        } else if options
            .repetition_limit
            .as_ref()
            .map(|opts| recent_similar >= opts.limit)
            .unwrap_or(false)
        {
            Err(BlockReason::Repetitious(recent_similar as usize))
        } else if options.block_if_severely_inappropriate
            && analysis.is(Type::INAPPROPRIATE & Type::SEVERE)
        {
            Err(BlockReason::Inappropriate(analysis))
        } else if let Some(remaining) = Self::remaining_duration(&mut self.only_safe_until, now)
            .filter(|_| !(analysis.is(Type::SAFE) || options.max_safe_timeout.is_zero()))
        {
            Err(BlockReason::Unsafe {
                remaining,
                targeted: true,
            })
        } else {
            self.last_message = Some(now);
            if let Some(rate_limit_options) = options.rate_limit.as_ref() {
                // How many messages does this count for against the rate limit.
                let rate_limit_messages =
                    if let Some(char_limit) = rate_limit_options.character_limit {
                        let char_count = message.chars().count();

                        #[cfg(feature = "width")]
                        let char_count = char_count.max(crate::width_str(&message));

                        (char_count / char_limit.get() as usize).clamp(1, 3) as u8
                    } else {
                        1
                    };

                self.burst_used = if remaining_rate_limit.is_some() {
                    self.burst_used.saturating_add(rate_limit_messages)
                } else {
                    self.burst_used.saturating_sub(
                        (elapsed.as_nanos() / rate_limit_options.limit.as_nanos())
                            .min(u8::MAX as u128) as u8,
                    )
                };
                if let Some(rate_limited_until) =
                    self.rate_limited_until.unwrap_or(now).checked_add(
                        rate_limit_options.limit * (rate_limit_messages + new_suspicion) as u32,
                    )
                {
                    self.rate_limited_until = Some(rate_limited_until);
                }
            }
            // Forgiveness (minus one suspicion per safe message, and also per minute between messages).
            self.suspicion = self.suspicion.saturating_sub(
                (elapsed.as_secs() / 60).clamp(analysis.is(Type::SAFE) as u64, u8::MAX as u64)
                    as u8,
            );

            if let Some(repetition_blocking_options) = options.repetition_limit.as_ref() {
                if self.history.len() >= repetition_blocking_options.limit as usize * 2 {
                    self.history.pop_front();
                }

                self.history.push_back((message, now));
            }

            Ok(censored)
        }
    }