VirtualBox

Changeset 2697 in kBuild


Ignore:
Timestamp:
Sep 16, 2013 10:30:08 PM (11 years ago)
Author:
bird
Message:

kmk: Extended the substr function to allow refering length from the end of the string by using negative numbers (just like the start offset).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/function.c

    r2594 r2697  
    28682868        }
    28692869      length = math_int_from_string (argv[2]);
    2870       if (length < 0 || (pad != NULL && length > 16*1024*1024 /* 16MB */))
    2871         fatal (NILF, _("$(substr ): length=%s is out of bounds\n"), argv[3]);
     2870      if (pad != NULL && length > 16*1024*1024 /* 16MB */)
     2871        fatal (NILF, _("$(substr ): length=%s is out of bounds\n"), argv[2]);
     2872      if (pad != NULL && length < 0)
     2873        fatal (NILF, _("$(substr ): negative length (%s) and padding doesn't mix.\n"), argv[2]);
    28722874      if (length == 0)
    28732875        return o;
    28742876    }
    28752877
    2876   /* adjust start and length. */
     2878  /* Note that negative start is and length are used for referencing from the
     2879     end of the string. */
    28772880  if (pad == NULL)
    28782881    {
    28792882      if (start > 0)
    2880         {
    2881           start--;      /* one-origin */
    2882           if (start >= str_len)
    2883             return o;
    2884           if (length == 0 || start + length > str_len)
    2885             length = str_len - start;
    2886         }
     2883        start--;      /* one-origin */
    28872884      else
    28882885        {
     
    28902887          if (start <= 0)
    28912888            {
     2889              if (length < 0)
     2890                return o;
    28922891              start += length;
    28932892              if (start <= 0)
     
    28962895              start = 0;
    28972896            }
    2898           else if (length == 0 || start + length > str_len)
    2899             length = str_len - start;
    2900         }
     2897        }
     2898
     2899      if (start >= str_len)
     2900        return o;
     2901      if (length == 0)
     2902        length = str_len - start;
     2903      else if (length < 0 && start >= -length)
     2904        return o;
     2905      else if (length < 0)
     2906        length = str_len - start + length;
     2907      else if (start + length > str_len)
     2908        length = str_len - start;
    29012909
    29022910      o = variable_buffer_output (o, str + start, length);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette