Skip to content

Commit

Permalink
strip whitespaces for subtext
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Aug 3, 2024
1 parent 4d413f4 commit b24abd6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 9 additions & 1 deletion credsweeper/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import math
import os
import string
import struct
import tarfile
from dataclasses import dataclass
Expand Down Expand Up @@ -685,6 +686,13 @@ def subtext(text: str, pos: int, hunk_size: int) -> str:
else:
left_quota = hunk_size - pos
left_pos = 0
# skip leading whitespaces in result string
for i in range(left_pos, pos):
if text[i] in string.whitespace:
left_quota += 1
left_pos += 1
else:
break
right_remain = len(text) - pos
if hunk_size <= right_remain:
right_quota = 0
Expand All @@ -698,4 +706,4 @@ def subtext(text: str, pos: int, hunk_size: int) -> str:
left_pos -= right_quota
if 0 > left_pos:
left_pos = 0
return text[left_pos:right_pos]
return text[left_pos:right_pos].rstrip()
16 changes: 11 additions & 5 deletions tests/utils/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,19 @@ def test_get_chunks_coverage_p(self):

def test_subtext_n(self):
self.assertEqual("", Util.subtext("", 0, 0))
self.assertEqual("", Util.subtext(' ' * 42, 0, 0))

def test_subtext_p(self):
# self.assertEqual(AZ_STRING, Util.subtext(AZ_STRING, 37, 40))
self.assertEqual("The quick ", Util.subtext(AZ_STRING, 0, 5))
self.assertEqual("The quick ", Util.subtext(AZ_STRING, 3, 5))
self.assertEqual(" fox jumps", Util.subtext(AZ_STRING, 20, 5))
self.assertEqual("var=value0123456789;", Util.subtext(" var=value0123456789; ", 21, 10))
self.assertEqual(AZ_STRING, Util.subtext(AZ_STRING, len(AZ_STRING) >> 1, 1 + len(AZ_STRING) >> 1))
self.assertEqual("x jump", Util.subtext(AZ_STRING, len(AZ_STRING) >> 1, 3))
self.assertEqual("ox jumps", Util.subtext(AZ_STRING, len(AZ_STRING) >> 1, 4))
self.assertEqual("fox jumps", Util.subtext(AZ_STRING, len(AZ_STRING) >> 1, 5))
self.assertEqual("fox jumps ov", Util.subtext(AZ_STRING, len(AZ_STRING) >> 1, 6))
self.assertEqual("The quick", Util.subtext(AZ_STRING, 0, 5))
self.assertEqual("The quick", Util.subtext(AZ_STRING, 3, 5))
self.assertEqual("fox jumps", Util.subtext(AZ_STRING, AZ_STRING.find("jumps"), 5))
self.assertEqual("e lazy dog", Util.subtext(AZ_STRING, len(AZ_STRING) - 2, 5))
self.assertEqual("the lazy dog", Util.subtext(AZ_STRING, len(AZ_STRING) - 2, 6))
self.assertEqual(AZ_STRING[:40], Util.subtext(AZ_STRING, 15, 20))
self.assertEqual(AZ_STRING[:39], Util.subtext(AZ_STRING, 15, 20))
self.assertEqual(AZ_STRING[-40:], Util.subtext(AZ_STRING, 33, 20))

0 comments on commit b24abd6

Please sign in to comment.