String.st
changeset 25098 787f66dc3405
parent 25018 6024e5e63311
child 25350 8f0794692a17
equal deleted inserted replaced
25097:e9734960752c 25098:787f66dc3405
  4225     REGISTER unsigned char c;
  4225     REGISTER unsigned char c;
  4226     REGISTER unsigned char *cp0;
  4226     REGISTER unsigned char *cp0;
  4227     REGISTER unsigned char *ep0;
  4227     REGISTER unsigned char *ep0;
  4228 
  4228 
  4229     /* ignore instances of subclasses ... */
  4229     /* ignore instances of subclasses ... */
  4230     if (__qClass(self) == String) {
  4230     if (__isStringLike(self)) {
  4231 	cp = cp0 = __stringVal(self);
  4231         cp = cp0 = __stringVal(self);
  4232 
  4232 
  4233 	/*
  4233         /*
  4234 	 * find first non-whiteSpace from beginning
  4234          * find first non-whiteSpace from beginning
  4235 	 */
  4235          */
  4236 #ifdef UINT64
  4236 #ifdef UINT64
  4237 	while (*((UINT64 *)cp) == 0x2020202020202020L) {
  4237         while (*((UINT64 *)cp) == 0x2020202020202020L) {
  4238 	    cp += 8;
  4238             cp += 8;
  4239 	}
  4239         }
  4240 #endif
  4240 #endif
  4241 	while (*((unsigned *)cp) == 0x20202020) {
  4241         while (*((unsigned *)cp) == 0x20202020) {
  4242 	    cp += 4;
  4242             cp += 4;
  4243 	}
  4243         }
  4244 	while ((c = *cp)
  4244         while ((c = *cp)
  4245 	 && (c <= ' ')
  4245          && (c <= ' ')
  4246 	 && ((c == ' ') || (c == '\n') || (c == '\t')
  4246          && ((c == ' ') || (c == '\n') || (c == '\t')
  4247 			|| (c == '\r') || (c == '\f'))
  4247                         || (c == '\r') || (c == '\f'))
  4248 	) {
  4248         ) {
  4249 	    cp++;
  4249             cp++;
  4250 	}
  4250         }
  4251 
  4251 
  4252 	/*
  4252         /*
  4253 	 * find first non-whiteSpace from end
  4253          * find first non-whiteSpace from end
  4254 	 */
  4254          */
  4255 	ep = ep0 = cp0 + __stringSize(self) - 1;
  4255         ep = ep0 = cp0 + __stringSize(self) - 1;
  4256 	while ((ep >= cp) && (*ep == ' ')) ep--;
  4256         while ((ep >= cp) && (*ep == ' ')) ep--;
  4257 	c = *ep;
  4257         c = *ep;
  4258 	while ((ep >= cp) &&
  4258         while ((ep >= cp) &&
  4259 	       (c <= ' ') &&
  4259                (c <= ' ') &&
  4260 	       ((c == ' ') || (c == '\n') || (c == '\t')
  4260                ((c == ' ') || (c == '\n') || (c == '\t')
  4261 			   || (c == '\r') || (c == '\f'))) {
  4261                            || (c == '\r') || (c == '\f'))) {
  4262 	    ep--;
  4262             ep--;
  4263 	    c = *ep;
  4263             c = *ep;
  4264 	}
  4264         }
  4265 
  4265 
  4266 	/*
  4266         /*
  4267 	 * no whiteSpace ?
  4267          * no whiteSpace ?
  4268 	 */
  4268          */
  4269 	if ((cp == cp0) && (ep == ep0)) {
  4269         if ((cp == cp0) && (ep == ep0)) {
  4270 	    RETURN(self);
  4270             RETURN(self);
  4271 	}
  4271         }
  4272 
  4272 
  4273 	startIndex = __mkSmallInteger(cp - cp0 + 1);
  4273         startIndex = __mkSmallInteger(cp - cp0 + 1);
  4274 	endIndex = __mkSmallInteger(ep - cp0 + 1);
  4274         endIndex = __mkSmallInteger(ep - cp0 + 1);
  4275     }
  4275     }
  4276 %}.
  4276 %}.
  4277     startIndex == 0 ifTrue:[^ super withoutSeparators].
  4277     startIndex == 0 ifTrue:[^ super withoutSeparators].
  4278 
  4278 
  4279     startIndex > endIndex ifTrue:[^ ''].
  4279     startIndex > endIndex ifTrue:[^ ''].
  4286      '    hello  ' withoutSeparators
  4286      '    hello  ' withoutSeparators
  4287      '    hello   ' withoutSeparators
  4287      '    hello   ' withoutSeparators
  4288      '    hello    ' withoutSeparators
  4288      '    hello    ' withoutSeparators
  4289      '        ' withoutSeparators
  4289      '        ' withoutSeparators
  4290     "
  4290     "
       
  4291 
       
  4292     "Modified: / 12-12-2019 / 14:16:51 / Stefan Vogel"
  4291 !
  4293 !
  4292 
  4294 
  4293 withoutSpaces
  4295 withoutSpaces
  4294     "return a string containing the characters of myself
  4296     "return a string containing the characters of myself
  4295      without leading and trailing spaces.
  4297      without leading and trailing spaces.
  4306     REGISTER unsigned char *ep;
  4308     REGISTER unsigned char *ep;
  4307     unsigned char *cp0;
  4309     unsigned char *cp0;
  4308     unsigned char *ep0;
  4310     unsigned char *ep0;
  4309 
  4311 
  4310     /* ignore instances of subclasses ... */
  4312     /* ignore instances of subclasses ... */
  4311     if (__qClass(self) == String) {
  4313     if (__isStringLike(self)) {
  4312 	cp = cp0 = __stringVal(self);
  4314         cp = cp0 = __stringVal(self);
  4313 
  4315 
  4314 	/*
  4316         /*
  4315 	 * find first non-blank from beginning
  4317          * find first non-blank from beginning
  4316 	 */
  4318          */
  4317 #ifdef UINT64
  4319 #ifdef UINT64
  4318 	while (*((UINT64 *)cp) == 0x2020202020202020L) {
  4320         while (*((UINT64 *)cp) == 0x2020202020202020L) {
  4319 	    cp += 8;
  4321             cp += 8;
  4320 	}
  4322         }
  4321 #endif /* UINT64 */
  4323 #endif /* UINT64 */
  4322 	while (*((unsigned *)cp) == 0x20202020) {
  4324         while (*((unsigned *)cp) == 0x20202020) {
  4323 	    cp += 4;
  4325             cp += 4;
  4324 	}
  4326         }
  4325 	while (*cp == ' ') cp++;
  4327         while (*cp == ' ') cp++;
  4326 
  4328 
  4327 	/*
  4329         /*
  4328 	 * find first non-blank from end
  4330          * find first non-blank from end
  4329 	 */
  4331          */
  4330 	ep = ep0 = cp0 + __stringSize(self) - 1;
  4332         ep = ep0 = cp0 + __stringSize(self) - 1;
  4331 	while ((ep >= cp) && (*ep == ' ')) ep--;
  4333         while ((ep >= cp) && (*ep == ' ')) ep--;
  4332 
  4334 
  4333 	/*
  4335         /*
  4334 	 * no blanks ?
  4336          * no blanks ?
  4335 	 */
  4337          */
  4336 	if ((cp == cp0) && (ep == ep0)) {
  4338         if ((cp == cp0) && (ep == ep0)) {
  4337 	    RETURN(self);
  4339             RETURN(self);
  4338 	}
  4340         }
  4339 
  4341 
  4340 	startIndex = __mkSmallInteger(cp - cp0 + 1);
  4342         startIndex = __mkSmallInteger(cp - cp0 + 1);
  4341 	endIndex = __mkSmallInteger(ep - cp0 + 1);
  4343         endIndex = __mkSmallInteger(ep - cp0 + 1);
  4342     }
  4344     }
  4343 %}.
  4345 %}.
  4344     startIndex == 0 ifTrue:[^ super withoutSpaces].
  4346     startIndex == 0 ifTrue:[^ super withoutSpaces].
  4345 
  4347 
  4346     startIndex > endIndex ifTrue:[^ ''].
  4348     startIndex > endIndex ifTrue:[^ ''].
  4353      '    hello  ' withoutSpaces
  4355      '    hello  ' withoutSpaces
  4354      '    hello   ' withoutSpaces
  4356      '    hello   ' withoutSpaces
  4355      '    hello    ' withoutSpaces
  4357      '    hello    ' withoutSpaces
  4356      '        ' withoutSpaces
  4358      '        ' withoutSpaces
  4357     "
  4359     "
       
  4360 
       
  4361     "Modified: / 12-12-2019 / 14:16:58 / Stefan Vogel"
       
  4362 !
       
  4363 
       
  4364 withoutTrailingSeparators
       
  4365     "return a copy of myself without trailing separators.
       
  4366      Notice: this does remove tabs, newline or any other whitespace.
       
  4367      Returns an empty string, if the receiver consist only of whitespace."
       
  4368 
       
  4369     |endIndex   "{ Class: SmallInteger }"|
       
  4370 
       
  4371     endIndex := -1.
       
  4372 %{
       
  4373     REGISTER unsigned char *cp0;
       
  4374     REGISTER unsigned char *ep;
       
  4375     REGISTER unsigned char c;
       
  4376     REGISTER unsigned char *ep0;
       
  4377 
       
  4378     /* ignore instances of subclasses ... */
       
  4379     if (__isStringLike(self)) {
       
  4380         cp0 = __stringVal(self);
       
  4381 
       
  4382         /*
       
  4383          * find first non-whiteSpace from end
       
  4384          */
       
  4385         ep = ep0 = cp0 + __stringSize(self) - 1;
       
  4386         while ((ep >= cp0) && (*ep == ' ')) ep--;
       
  4387         c = *ep;
       
  4388         while ((ep >= cp0) &&
       
  4389                (c <= ' ') &&
       
  4390                ((c == ' ') || (c == '\n') || (c == '\t')
       
  4391                            || (c == '\r') || (c == '\f'))) {
       
  4392             ep--;
       
  4393             c = *ep;
       
  4394         }
       
  4395 
       
  4396         /*
       
  4397          * no whiteSpace ?
       
  4398          */
       
  4399         if (ep == ep0) {
       
  4400             RETURN(self);
       
  4401         }
       
  4402 
       
  4403         endIndex = __mkSmallInteger(ep - cp0 + 1);
       
  4404     }
       
  4405 %}.
       
  4406     endIndex == -1 ifTrue:[^ super withoutTrailingSeparators].
       
  4407     endIndex == 0 ifTrue:[^ ''].
       
  4408     ^ self copyFrom:1 to:endIndex
       
  4409 
       
  4410 
       
  4411     "
       
  4412      '    foo    ' withoutTrailingSeparators
       
  4413      'foo    '     withoutTrailingSeparators
       
  4414      '    foo'     withoutTrailingSeparators
       
  4415      '       '     withoutTrailingSeparators
       
  4416      'foo'         withoutTrailingSeparators
       
  4417      'f'           withoutTrailingSeparators
       
  4418      'f '          withoutTrailingSeparators
       
  4419      ''            withoutTrailingSeparators
       
  4420      ('  ' , Character tab , ' foo   ') withoutTrailingSeparators inspect
       
  4421      ('   foo' , Character tab) withoutTrailingSeparators inspect
       
  4422     "
       
  4423 
       
  4424     "Created: / 12-12-2019 / 14:00:23 / Stefan Vogel"
  4358 ! !
  4425 ! !
  4359 
  4426 
  4360 !String methodsFor:'printing & storing'!
  4427 !String methodsFor:'printing & storing'!
  4361 
  4428 
  4362 _errorPrint
  4429 _errorPrint