aboutsummaryrefslogtreecommitdiffstats
path: root/lib/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c57
1 files changed, 55 insertions, 2 deletions
diff --git a/lib/string.c b/lib/string.c
index 01fad9b203e1..dc4a86341f91 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -360,7 +360,6 @@ char *strim(char *s)
360 size_t size; 360 size_t size;
361 char *end; 361 char *end;
362 362
363 s = skip_spaces(s);
364 size = strlen(s); 363 size = strlen(s);
365 if (!size) 364 if (!size)
366 return s; 365 return s;
@@ -370,7 +369,7 @@ char *strim(char *s)
370 end--; 369 end--;
371 *(end + 1) = '\0'; 370 *(end + 1) = '\0';
372 371
373 return s; 372 return skip_spaces(s);
374} 373}
375EXPORT_SYMBOL(strim); 374EXPORT_SYMBOL(strim);
376 375
@@ -756,3 +755,57 @@ void *memchr(const void *s, int c, size_t n)
756} 755}
757EXPORT_SYMBOL(memchr); 756EXPORT_SYMBOL(memchr);
758#endif 757#endif
758
759static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
760{
761 while (bytes) {
762 if (*start != value)
763 return (void *)start;
764 start++;
765 bytes--;
766 }
767 return NULL;
768}
769
770/**
771 * memchr_inv - Find an unmatching character in an area of memory.
772 * @start: The memory area
773 * @c: Find a character other than c
774 * @bytes: The size of the area.
775 *
776 * returns the address of the first character other than @c, or %NULL
777 * if the whole buffer contains just @c.
778 */
779void *memchr_inv(const void *start, int c, size_t bytes)
780{
781 u8 value = c;
782 u64 value64;
783 unsigned int words, prefix;
784
785 if (bytes <= 16)
786 return check_bytes8(start, value, bytes);
787
788 value64 = value | value << 8 | value << 16 | value << 24;
789 value64 = (value64 & 0xffffffff) | value64 << 32;
790 prefix = 8 - ((unsigned long)start) % 8;
791
792 if (prefix) {
793 u8 *r = check_bytes8(start, value, prefix);
794 if (r)
795 return r;
796 start += prefix;
797 bytes -= prefix;
798 }
799
800 words = bytes / 8;
801
802 while (words) {
803 if (*(u64 *)start != value64)
804 return check_bytes8(start, value, 8);
805 start += 8;
806 words--;
807 }
808
809 return check_bytes8(start, value, bytes % 8);
810}
811EXPORT_SYMBOL(memchr_inv);