PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ stringbuffer_trim_trailing_zeroes()

int stringbuffer_trim_trailing_zeroes ( stringbuffer_t s)

Trims zeroes off the end of the last number in the stringbuffer.

The number has to be the very last thing in the buffer. Only the last number will be trimmed. Returns the number of characters trimmed.

eg: 1.22000 -> 1.22 1.0 -> 1 0.0 -> 0

Definition at line 279 of file stringbuffer.c.

280 {
281  char *ptr = s->str_end;
282  char *decimal_ptr = NULL;
283  int dist;
284 
285  if ( s->str_end - s->str_start < 2)
286  return 0;
287 
288  /* Roll backwards to find the decimal for this number */
289  while( ptr > s->str_start )
290  {
291  ptr--;
292  if ( *ptr == '.' )
293  {
294  decimal_ptr = ptr;
295  break;
296  }
297  if ( (*ptr >= '0') && (*ptr <= '9' ) )
298  continue;
299  else
300  break;
301  }
302 
303  /* No decimal? Nothing to trim! */
304  if ( ! decimal_ptr )
305  return 0;
306 
307  ptr = s->str_end;
308 
309  /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
310  while( ptr >= decimal_ptr )
311  {
312  ptr--;
313  if ( *ptr == '0' )
314  continue;
315  else
316  break;
317  }
318 
319  /* Huh, we get anywhere. Must not have trimmed anything. */
320  if ( ptr == s->str_end )
321  return 0;
322 
323  /* If we stopped at the decimal, we want to null that out.
324  It we stopped on a numeral, we want to preserve that, so push the
325  pointer forward one space. */
326  if ( *ptr != '.' )
327  ptr++;
328 
329  /* Add null terminator re-set the end of the stringbuffer. */
330  *ptr = '\0';
331  dist = s->str_end - ptr;
332  s->str_end = ptr;
333  return dist;
334 }
char * s
Definition: cu_in_wkt.c:23

References s.