mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Right size literal regular expression buffers on compile
This commit is contained in:
		
							parent
							
								
									e7433a3288
								
							
						
					
					
						commit
						9a17437558
					
				
				
				Notes:
				
					git
				
				2020-12-10 18:15:51 +09:00 
				
			
			
			
		
		
					 1 changed files with 20 additions and 0 deletions
				
			
		
							
								
								
									
										20
									
								
								regcomp.c
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								regcomp.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -138,6 +138,25 @@ bitset_on_num(BitSetRef bs)
 | 
			
		|||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
// Attempt to right size allocated buffers for a regex post compile
 | 
			
		||||
static void
 | 
			
		||||
onig_reg_resize(regex_t *reg)
 | 
			
		||||
{
 | 
			
		||||
  resize:
 | 
			
		||||
    if (reg->alloc > reg->used) {
 | 
			
		||||
      unsigned char *new_ptr = xrealloc(reg->p, reg->used);
 | 
			
		||||
      // Skip the right size optimization if memory allocation fails
 | 
			
		||||
      if (new_ptr) {
 | 
			
		||||
        reg->alloc = reg->used;
 | 
			
		||||
        reg->p = new_ptr;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    if (reg->chain) {
 | 
			
		||||
      reg = reg->chain;
 | 
			
		||||
      goto resize;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern int
 | 
			
		||||
onig_bbuf_init(BBuf* buf, OnigDistance size)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -5886,6 +5905,7 @@ onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end,
 | 
			
		|||
#endif
 | 
			
		||||
 | 
			
		||||
 end:
 | 
			
		||||
  onig_reg_resize(reg);
 | 
			
		||||
  return r;
 | 
			
		||||
 | 
			
		||||
 err_unset:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue