44
55from dictdatabase import byte_codes
66from dictdatabase import utils
7+ from dictdatabase .dataclasses import SearchResult
78
89
9- def find_key_position_in_bytes (file : bytes , key : str ) -> Tuple [ int , int , bool ] :
10+ def find_key_position_in_bytes (file : bytes , key : str ) -> SearchResult :
1011 """
1112 It finds the start and end indices of the value of a key in a JSON file
1213
@@ -19,28 +20,30 @@ def find_key_position_in_bytes(file: bytes, key: str) -> Tuple[int, int, bool]:
1920 """
2021 key_start , key_end = utils .find_outermost_key_in_json_bytes (file , key )
2122 if key_end == - 1 :
22- return - 1 , - 1 , False
23+ return SearchResult ( start_byte = - 1 , end_byte = - 1 , found = False )
2324 start = key_end + (1 if file [key_end ] == byte_codes .SPACE else 0 )
2425 end = utils .seek_index_through_value_bytes (file , start )
25- return start , end , True
26+ return SearchResult ( start_byte = start , end_byte = end , found = True )
2627
2728
28- def search_key_position_in_db (file : bytes , key : str , glom_searching = True ) -> Tuple [int , int , bool ]:
29+ def search_key_position_in_db (
30+ file : bytes , key : str , glom_searching = True
31+ ) -> SearchResult :
2932 original_value_start = 0
3033 original_value_end = len (file )
3134 original_key_start = 0
3235 original_key_end = len (file )
3336 for k in key .split ("." ) if glom_searching else [key ]:
3437 key_start , key_end = utils .find_outermost_key_in_json_bytes (file , k )
3538 if key_end == - 1 :
36- return - 1 , - 1 , False
39+ return SearchResult ( start_byte = - 1 , end_byte = - 1 , found = False )
3740 original_key_end = original_value_start + key_end
3841 original_key_start = original_value_start + key_start
39- value_start , value_end , found = find_key_position_in_bytes (file , k )
42+ position = find_key_position_in_bytes (file , k )
4043 original_value_end = original_value_start + original_value_end
41- original_value_start += value_start
44+ original_value_start += position . start_byte
4245 file = file [original_value_start :original_value_end ]
43- return original_key_start , original_key_end , True
46+ return SearchResult ( start_byte = original_key_start , end_byte = original_key_end , found = True )
4447
4548
4649def search_value_position_in_db (
@@ -61,11 +64,11 @@ def search_value_position_in_db(
6164 original_start = 0
6265 original_end = len (all_file_bytes )
6366 for k in key .split ("." ) if glom_searching else [key ]:
64- start , end , found = find_key_position_in_bytes (
67+ position = find_key_position_in_bytes (
6568 all_file_bytes [original_start :original_end ], k
6669 )
67- if not found :
70+ if not position . found :
6871 return - 1 , - 1 , False
69- original_end = original_start + end
70- original_start += start
72+ original_end = original_start + position . end_byte
73+ original_start += position . start_byte
7174 return original_start , original_end , True
0 commit comments