1 /* |
1 /* |
2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. |
2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
23 */ |
23 */ |
24 |
24 |
25 # include "incls/_precompiled.incl" |
25 # include "incls/_precompiled.incl" |
26 # include "incls/_symbolOop.cpp.incl" |
26 # include "incls/_symbolOop.cpp.incl" |
27 |
27 |
|
28 |
|
29 // ------------------------------------------------------------------ |
|
30 // symbolOopDesc::equals |
|
31 // |
|
32 // Compares the symbol with a string of the given length. |
28 bool symbolOopDesc::equals(const char* str, int len) const { |
33 bool symbolOopDesc::equals(const char* str, int len) const { |
29 int l = utf8_length(); |
34 int l = utf8_length(); |
30 if (l != len) return false; |
35 if (l != len) return false; |
31 while (l-- > 0) { |
36 while (l-- > 0) { |
32 if (str[l] != (char) byte_at(l)) |
37 if (str[l] != (char) byte_at(l)) |
33 return false; |
38 return false; |
34 } |
39 } |
35 assert(l == -1, "we should be at the beginning"); |
40 assert(l == -1, "we should be at the beginning"); |
36 return true; |
41 return true; |
37 } |
42 } |
|
43 |
|
44 |
|
45 // ------------------------------------------------------------------ |
|
46 // symbolOopDesc::starts_with |
|
47 // |
|
48 // Tests if the symbol starts with the specified prefix of the given |
|
49 // length. |
|
50 bool symbolOopDesc::starts_with(const char* prefix, int len) const { |
|
51 if (len > utf8_length()) return false; |
|
52 while (len-- > 0) { |
|
53 if (prefix[len] != (char) byte_at(len)) |
|
54 return false; |
|
55 } |
|
56 assert(len == -1, "we should be at the beginning"); |
|
57 return true; |
|
58 } |
|
59 |
|
60 |
|
61 // ------------------------------------------------------------------ |
|
62 // symbolOopDesc::index_of |
|
63 // |
|
64 // Finds if the given string is a substring of this symbol's utf8 bytes. |
|
65 // Return -1 on failure. Otherwise return the first index where str occurs. |
|
66 int symbolOopDesc::index_of_at(int i, const char* str, int len) const { |
|
67 assert(i >= 0 && i <= utf8_length(), "oob"); |
|
68 if (len <= 0) return 0; |
|
69 char first_char = str[0]; |
|
70 address bytes = (address) ((symbolOopDesc*)this)->base(); |
|
71 address limit = bytes + utf8_length() - len; // inclusive limit |
|
72 address scan = bytes + i; |
|
73 if (scan > limit) |
|
74 return -1; |
|
75 for (;;) { |
|
76 scan = (address) memchr(scan, first_char, (limit + 1 - scan)); |
|
77 if (scan == NULL) |
|
78 return -1; // not found |
|
79 assert(scan >= bytes+i && scan <= limit, "scan oob"); |
|
80 if (memcmp(scan, str, len) == 0) |
|
81 return (int)(scan - bytes); |
|
82 } |
|
83 } |
|
84 |
38 |
85 |
39 char* symbolOopDesc::as_C_string(char* buf, int size) const { |
86 char* symbolOopDesc::as_C_string(char* buf, int size) const { |
40 if (size > 0) { |
87 if (size > 0) { |
41 int len = MIN2(size - 1, utf8_length()); |
88 int len = MIN2(size - 1, utf8_length()); |
42 for (int i = 0; i < len; i++) { |
89 for (int i = 0; i < len; i++) { |