mirror of
				https://github.com/stedolan/jq.git
				synced 2024-05-11 05:55:39 +00:00 
			
		
		
		
	'length' function now measures string length in codepoints, not bytes.
This commit is contained in:
		
							
								
								
									
										13
									
								
								jv.c
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								jv.c
									
									
									
									
									
								
							@@ -8,6 +8,7 @@
 | 
			
		||||
 | 
			
		||||
#include "jv_alloc.h"
 | 
			
		||||
#include "jv.h"
 | 
			
		||||
#include "jv_unicode.h"
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Internal refcounting helpers
 | 
			
		||||
@@ -530,13 +531,23 @@ jv jv_string(const char* str) {
 | 
			
		||||
  return jv_string_sized(str, strlen(str));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int jv_string_length(jv j) {
 | 
			
		||||
int jv_string_length_bytes(jv j) {
 | 
			
		||||
  assert(jv_get_kind(j) == JV_KIND_STRING);
 | 
			
		||||
  int r = jvp_string_length(jvp_string_ptr(&j.val.nontrivial));
 | 
			
		||||
  jv_free(j);
 | 
			
		||||
  return r;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int jv_string_length_codepoints(jv j) {
 | 
			
		||||
  assert(jv_get_kind(j) == JV_KIND_STRING);
 | 
			
		||||
  const char* i = jv_string_value(j);
 | 
			
		||||
  const char* end = i + jv_string_length_bytes(jv_copy(j));
 | 
			
		||||
  int c = 0, len = 0;
 | 
			
		||||
  while ((i = jvp_utf8_next(i, end, &c))) len++;
 | 
			
		||||
  jv_free(j);
 | 
			
		||||
  return len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t jv_string_hash(jv j) {
 | 
			
		||||
  assert(jv_get_kind(j) == JV_KIND_STRING);
 | 
			
		||||
  uint32_t hash = jvp_string_hash(jvp_string_ptr(&j.val.nontrivial));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user