Rectangle 27 401

The standard tool for listing symbols is nm, you can use it simply like this:

nm -g yourLib.so

If you want to see symbols of a C++ library, add the "-C" option which demangle the symbols (it's far more readable demangled).

nm -gC yourLib.so

If your .so file is in elf format, you have two options:

objdump
-C
$ objdump -TC libz.so

libz.so:     file format elf64-x86-64

DYNAMIC SYMBOL TABLE:
0000000000002010 l    d  .init  0000000000000000              .init
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 free
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 __errno_location
0000000000000000  w   D  *UND*  0000000000000000              _ITM_deregisterTMCloneTable
readelf
$ readelf -Ws libz.so
Symbol table '.dynsym' contains 112 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000002010     0 SECTION LOCAL  DEFAULT   10
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND free@GLIBC_2.2.5 (14)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __errno_location@GLIBC_2.2.5 (14)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable

This doesn't always work with .so files, though, and so you may have to use the "readelf" solution mentioned in another answer.

Note that OS X versions of nm are missing the '-C' option for demangling symbols. c++filt can be used instead. Example script here: v8.googlecode.com/svn/branches/bleeding_edge/tools/mac-nm nm -g /usr/lib/libstdc++.6.dylib | c++filt -p -i

Note that readelf -Ws will show you all symbols, and nm -g shows only the externally visible symbols. This may be confusing if you are examining multiple symbol files and start interchanging your commands.

I would also add objectdump -TC to the list. In contrary to readelf -Ws, it doesn't show the mangled names.

Unknown command line argument '-gC'

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

c++ - How do I list the symbols in a .so file - Stack Overflow

c++ c gcc symbols name-mangling
Rectangle 27 396

The standard tool for listing symbols is nm, you can use it simply like this:

nm -g yourLib.so

If you want to see symbols of a C++ library, add the "-C" option which demangle the symbols (it's far more readable demangled).

nm -gC yourLib.so

If your .so file is in elf format, you have two options:

objdump
-C
$ objdump -TC libz.so

libz.so:     file format elf64-x86-64

DYNAMIC SYMBOL TABLE:
0000000000002010 l    d  .init  0000000000000000              .init
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 free
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 __errno_location
0000000000000000  w   D  *UND*  0000000000000000              _ITM_deregisterTMCloneTable
readelf
$ readelf -Ws libz.so
Symbol table '.dynsym' contains 112 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000002010     0 SECTION LOCAL  DEFAULT   10
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND free@GLIBC_2.2.5 (14)
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __errno_location@GLIBC_2.2.5 (14)
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable

This doesn't always work with .so files, though, and so you may have to use the "readelf" solution mentioned in another answer.

Note that OS X versions of nm are missing the '-C' option for demangling symbols. c++filt can be used instead. Example script here: v8.googlecode.com/svn/branches/bleeding_edge/tools/mac-nm nm -g /usr/lib/libstdc++.6.dylib | c++filt -p -i

Note that readelf -Ws will show you all symbols, and nm -g shows only the externally visible symbols. This may be confusing if you are examining multiple symbol files and start interchanging your commands.

I would also add objectdump -TC to the list. In contrary to readelf -Ws, it doesn't show the mangled names.

Unknown command line argument '-gC'

c++ - How do I list the symbols in a .so file - Stack Overflow

c++ c gcc symbols name-mangling
Rectangle 27 1

  • Doug Lea's "JSR-133 cookbook" which gives information for compiler writers on how to implement the memory model
  • The Java Memory Model mailing list archive, where you can read through some of the arguments that were flying about that led to the current revision of the JMM.

On the JMM mailing list: Crap! That's a lot of reading!

sorry, a stray character got into the link -- fixed now

p.s. the mailing list is searchable!

Oh, and yes, whatever you do, it's a lot of reading, because this is quite complicated stuff. That's why you have JVMs and program in Java rather than assembler, so that in general, you don't have to worry too much about all of this nitty-gritty (though I confess I'm nerdy enough to also find it interesting).

synchronization - What is the scope of cache flushing required by Java...

java synchronization
Rectangle 27 63

If your .so file is in elf format, you can use readelf program to extract symbol information from the binary. This command will give you the symbol table:

readelf -Ws /usr/lib/libexample.so

You only should extract those that are defined in this .so file, not in the libraries referenced by it. Seventh column should contain a number in this case. You can extract it by using a simple regex:

or, as proposed by Caspin,:

readelf -Ws /usr/lib/libstdc++.so.6 | awk '{print $8}';

readelf -Ws /usr/lib/libstdc++.so.6 | awk '{print $8}'; regexes are awesome but sometimes a little awk goes a long way.

c++ - How do I list the symbols in a .so file - Stack Overflow

c++ c gcc symbols name-mangling
Rectangle 27 63

If your .so file is in elf format, you can use readelf program to extract symbol information from the binary. This command will give you the symbol table:

readelf -Ws /usr/lib/libexample.so

You only should extract those that are defined in this .so file, not in the libraries referenced by it. Seventh column should contain a number in this case. You can extract it by using a simple regex:

or, as proposed by Caspin,:

readelf -Ws /usr/lib/libstdc++.so.6 | awk '{print $8}';

readelf -Ws /usr/lib/libstdc++.so.6 | awk '{print $8}'; regexes are awesome but sometimes a little awk goes a long way.

c++ - How do I list the symbols in a .so file - Stack Overflow

c++ c gcc symbols name-mangling
Rectangle 27 451

Here is an example of how to use it (you may specify either full path or just file name):

String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt"); // returns "txt"
String ext2 = FilenameUtils.getExtension("bar.exe"); // returns "exe"

Should be noted that it returns only "gz" for a file named archive.tar.gz.

@Zitrax that's because "gz" is the file extension.

@BrainSlugs83 So what does mean "tar"?

@zhelon .gz stands for gnu zipped file, and .tar stands for (t)ape (ar)chive. So .tar.gz is a tar file inside a gnu zipped file, which has the .gz extension.

@guru_001 No it's not of course, it's just to mention that you may call it both with full path or just file name.

How do I get the file extension of a file in Java? - Stack Overflow

java file io
Rectangle 27 449

Here is an example of how to use it (you may specify either full path or just file name):

String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt"); // returns "txt"
String ext2 = FilenameUtils.getExtension("bar.exe"); // returns "exe"

Should be noted that it returns only "gz" for a file named archive.tar.gz.

@Zitrax that's because "gz" is the file extension.

@BrainSlugs83 So what does mean "tar"?

@zhelon .gz stands for gnu zipped file, and .tar stands for (t)ape (ar)chive. So .tar.gz is a tar file inside a gnu zipped file, which has the .gz extension.

@guru_001 No it's not of course, it's just to mention that you may call it both with full path or just file name.

How do I get the file extension of a file in Java? - Stack Overflow

java file io
Rectangle 27 153

I've just been fighting with this today. Let's call the behavior of removing the extra line breaks "continuation." A little experimenting finds the following behavior:

  • Lines less than 40 characters long do not trigger continuation, but if continuation is on, they will have their line breaks removed.
  • Lines 40 characters or longer turn continuation on. It remains on until an event occurs to turn it off.
  • Lines that end with a period, question mark, exclamation point or colon turn continuation off. (Outlook assumes it's the end of a sentence?)
  • Lines that turn continuation off will start with a line break, but will turn continuation back on if they are longer than 40 characters.
  • Lines that start or end with a tab turn continuation off.
  • Lines that start with 2 or more spaces turn continuation off.
  • Lines that end with 3 or more spaces turn continuation off.

Please note that I tried all of this with Outlook 2007. YMMV. So if possible, end all bullet items with a sentence-terminating punctuation mark, a tab, or even three spaces.

Marvellous work! Same in Outlook 2010.

Really good answer. Especially the fact that 3 spaces at the end stop outlook is good to know (and not found in many places on the internet).

This is the answer that should be automatically displayed as a startup screen when opening outlook.

Why is this not documented anywhere? This is a very useful piece of information to have. Thank you for doing the research!

+1 for "Lines that end with 3 or more spaces turn continuation off." This has been effective for me in order to ensure Outlook renders a new line for short lines.

java - How do I format a String in an email so Outlook will print the ...

java email outlook newline line-breaks
Rectangle 27 153

I've just been fighting with this today. Let's call the behavior of removing the extra line breaks "continuation." A little experimenting finds the following behavior:

  • Lines less than 40 characters long do not trigger continuation, but if continuation is on, they will have their line breaks removed.
  • Lines 40 characters or longer turn continuation on. It remains on until an event occurs to turn it off.
  • Lines that end with a period, question mark, exclamation point or colon turn continuation off. (Outlook assumes it's the end of a sentence?)
  • Lines that turn continuation off will start with a line break, but will turn continuation back on if they are longer than 40 characters.
  • Lines that start or end with a tab turn continuation off.
  • Lines that start with 2 or more spaces turn continuation off.
  • Lines that end with 3 or more spaces turn continuation off.

Please note that I tried all of this with Outlook 2007. YMMV. So if possible, end all bullet items with a sentence-terminating punctuation mark, a tab, or even three spaces.

Marvellous work! Same in Outlook 2010.

Really good answer. Especially the fact that 3 spaces at the end stop outlook is good to know (and not found in many places on the internet).

This is the answer that should be automatically displayed as a startup screen when opening outlook.

Why is this not documented anywhere? This is a very useful piece of information to have. Thank you for doing the research!

+1 for "Lines that end with 3 or more spaces turn continuation off." This has been effective for me in order to ensure Outlook renders a new line for short lines.

java - How do I format a String in an email so Outlook will print the ...

java email outlook newline line-breaks
Rectangle 27 767

How about this. It doesn't use regexp underneath so should be faster than some of the other solutions and won't use a loop.

int count = line.length() - line.replace(".", "").length();

Easiest way. Clever one. And it works on Android, where there is no StringUtils class

This is the best answer. The reason it is the best is because you don't have to import another library.

Very practical but ugly as hell. I don't recommend it as it leads to confusing code.

Ugly code can be minimized by making it a method in your own "StringUtils" class. Then the ugly code is in exactly one spot, and everywhere else is nicely readable.

The loop method is much faster than this. Especially when wanting to count a char instead of a String (since there is no String.replace(char, char) method). On a 15 character string, I get a difference of 6049 ns vs 26,739 ns (averaged over 100runs). Raw numbers are huge difference, but percetage wise...it adds up. Avoid the memory allocations - use a loop!

Java: How do I count the number of occurrences of a char in a String? ...

java string
Rectangle 27 213

Do you really need a "parser" for this?

Assuming that you're dealing with simple Windows-like file names, not something like archive.tar.gz.

Btw, for the case that a directory may have a '.', but the filename itself doesn't (like /path/to.a/file), you can do

String extension = "";

int i = fileName.lastIndexOf('.');
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));

if (i > p) {
    extension = fileName.substring(i+1);
}

Thanks! Sure you might need a parser/object for this, if you wanted to do more manipulations than just the extension... say if you want just the path, the parent directory, the file name (minus the extension), etc. I'm coming from C# and .Net where we have this: msdn.microsoft.com/en-us/library/

As you say, there are a number of things to think about, beyond just using the naive lastIndexOf("."). I would guess that Apache Commons has a method for this, which takes all of the little tricky potential problems into account.

I think i > 0 should be changed to i >= 0 or i != -1. This takes care of filenames like .htaccess.

regardless of how simple any code snippet is... you still need to update it/maintain it/test it/make it available as a convenient dependency... much easier if there was already a lib doing all of that

How do I get the file extension of a file in Java? - Stack Overflow

java file io
Rectangle 27 212

Do you really need a "parser" for this?

Assuming that you're dealing with simple Windows-like file names, not something like archive.tar.gz.

Btw, for the case that a directory may have a '.', but the filename itself doesn't (like /path/to.a/file), you can do

String extension = "";

int i = fileName.lastIndexOf('.');
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));

if (i > p) {
    extension = fileName.substring(i+1);
}

Thanks! Sure you might need a parser/object for this, if you wanted to do more manipulations than just the extension... say if you want just the path, the parent directory, the file name (minus the extension), etc. I'm coming from C# and .Net where we have this: msdn.microsoft.com/en-us/library/

As you say, there are a number of things to think about, beyond just using the naive lastIndexOf("."). I would guess that Apache Commons has a method for this, which takes all of the little tricky potential problems into account.

I think i > 0 should be changed to i >= 0 or i != -1. This takes care of filenames like .htaccess.

regardless of how simple any code snippet is... you still need to update it/maintain it/test it/make it available as a convenient dependency... much easier if there was already a lib doing all of that

How do I get the file extension of a file in Java? - Stack Overflow

java file io
Rectangle 27 567

int count = StringUtils.countMatches("a.b.c.d", ".");
int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");
int count = CharMatcher.is('.').countIn("a.b.c.d");

Although i will not downvote this, it is (a) requiring 3rd party libs and (b) expensive.

Java: How do I count the number of occurrences of a char in a String? ...

java string
Rectangle 27 153

Sooner or later, something has to loop. It's far simpler for you to write the (very simple) loop than to use something like split which is much more powerful than you need.

By all means encapsulate the loop in a separate method, e.g.

public static int countOccurrences(String haystack, char needle)
{
    int count = 0;
    for (int i=0; i < haystack.length(); i++)
    {
        if (haystack.charAt(i) == needle)
        {
             count++;
        }
    }
    return count;
}

Then you don't need have the loop in your main code - but the loop has to be there somewhere.

for (int i=0,l=haystack.length(); i < l; i++) be kind to your stack

(I'm not even sure where the "stack" bit of the comment comes from. It's not like this answer is my recursive one, which is indeed nasty to the stack.)

not only that but this is possibly an anti optimization without taking a look at what the jit does. If you did the above on an array for loop for example you might make things worse.

@sulai: Chris's concern is baseless, IMO, in the face of a trivial JIT optimization. Is there any reason that comment drew your attention at the moment, over three years later? Just interested.

Probably @sulai just came across the question as I did (while wondering if Java had a built-in method for this) and didn't notice the dates. However, I'm curious how moving the length() call outside of the loop could make performance worse, as mentioned by @ShuggyCoUk a few comments up.

Java: How do I count the number of occurrences of a char in a String? ...

java string
Rectangle 27 64

If you use Guava library, you can resort to Files utility class. It has a specific method, getFileExtension(). For instance:

String path = "c:/path/to/file/foo.txt";
String ext = Files.getFileExtension(path);
System.out.println(ext); //prints txt

In addition you may also obtain the filename with a similar function, getNameWithoutExtension():

String filename = Files.getNameWithoutExtension(path);
System.out.println(filename); //prints foo

Really? It is a great library, full of utilities. Most of them will be part of Java8, like the great Guava Function.

How do I get the file extension of a file in Java? - Stack Overflow

java file io
Rectangle 27 64

If you use Guava library, you can resort to Files utility class. It has a specific method, getFileExtension(). For instance:

String path = "c:/path/to/file/foo.txt";
String ext = Files.getFileExtension(path);
System.out.println(ext); //prints txt

In addition you may also obtain the filename with a similar function, getNameWithoutExtension():

String filename = Files.getNameWithoutExtension(path);
System.out.println(filename); //prints foo

Really? It is a great library, full of utilities. Most of them will be part of Java8, like the great Guava Function.

How do I get the file extension of a file in Java? - Stack Overflow

java file io
Rectangle 27 143

String testString = "a.b.c.d";
int apache = StringUtils.countMatches(testString, ".");
System.out.println("apache = " + apache);
int spring = org.springframework.util.StringUtils.countOccurrencesOf(testString, ".");
System.out.println("spring = " + spring);
int replace = testString.length() - testString.replace(".", "").length();
System.out.println("replace = " + replace);
int replaceAll = testString.replaceAll("[^.]", "").length();
System.out.println("replaceAll = " + replaceAll);
int replaceAllCase2 = testString.length() - testString.replaceAll("\\.", "").length();
System.out.println("replaceAll (second case) = " + replaceAllCase2);
int split = testString.split("\\.",-1).length-1;
System.out.println("split = " + split);
long java8 = testString.chars().filter(ch -> ch =='.').count();
System.out.println("java8 = " + java8);
long java8Case2 = testString.codePoints().filter(ch -> ch =='.').count();
System.out.println("java8 (second case) = " + java8Case2);
int stringTokenizer = new StringTokenizer(" " +testString + " ", ".").countTokens()-1;
System.out.println("stringTokenizer = " + stringTokenizer);

From comment: Be carefull for the StringTokenizer, for a.b.c.d it will work but for a...b.c....d or ...a.b.c.d or a....b......c.....d... or etc. it will not work. It just will count for . between characters just once

More info in github

Perfomance test (using JMH, mode = AverageTime, score 0.010 better then 0.351):

Benchmark              Mode  Cnt  Score    Error  Units
1. countMatches        avgt    5  0.010   0.001  us/op
2. countOccurrencesOf  avgt    5  0.010   0.001  us/op
3. stringTokenizer     avgt    5  0.028   0.002  us/op
4. java8_1             avgt    5  0.077   0.005  us/op
5. java8_2             avgt    5  0.078   0.003  us/op
6. split               avgt    5  0.137   0.009  us/op
7. replaceAll_2        avgt    5  0.302   0.047  us/op
8. replace             avgt    5  0.303   0.034  us/op
9. replaceAll_1        avgt    5  0.351   0.045  us/op

The printed strings do not match the ones above, and the order is fastest first which makes lookup tricky at least. Nice answer otherways!

Java: How do I count the number of occurrences of a char in a String? ...

java string
Rectangle 27 33

which should make it pass validation. To be extra safe you can add Javascript comments around the CDATA tags to hide them from older browsers who don't understand the CDATA tag:

<script type="text/javascript">
/* <![CDATA[ */
// content of your Javascript goes here
/* ]]> */
</script>

That. Or maybe use \u0026 (i think that's the code for &)

That was perfect. The first answer didn't work but the second one did. Thanks for the help!

jquery - How do I escape an ampersand in a javascript string so that t...

javascript jquery ajax w3c-validation
Rectangle 27 7

The compiler is not allowed to optimize out this_thread::yield() unless it can determine that it's free of side-effects -- which it can't, because it's not.

The atomic load operation can't be optimized away, because it includes a memory barrier and is specifically defined to pick up modifications made by other threads.

Thanks for this, though from what I've read, this_thread::yield() is implementation defined, and since it's mostly a hint, it could be a NOP, which in turn would be free of side-effects and optimizable. At least that's what I got from reading the standard.

If it's a NOP, then it doesn't matter if it's optimized out. It still has to run the loop to do the atomic load

Just because a function is a NOP doesn't mean the compiler can optimize it away. That's only possible if the function definition is visible to the compiler, which typically means it's inline, or you're doing interprecedural optimization (and it's unlikely that your C or C++ run-time provides the necessary info to enable IPO).

@JonathanWakely So if I have a call to a non-inline function that is completely empty, most compilers won't remove the call altogether? Or am I misinterpreting what you're saying?

That's what I'm saying. How can the compiler remove a call to a function if it doesn't know what the function does? (It doesn't necessarily have to be inline, but the function body has to be visible in the translation unit being compiled, which is only true for functions defined in the same translation unit, inline or not).

c++ - How do I implement a simple spinlock on std::atomic so that t...

c++ multithreading c++11 optimization
Rectangle 27 25

You can force a line break in outlook when attaching one (or two?) tab characters (\t) just before the line break (CRLF).

This is my heading in the mail\t\n
Just here Outlook is forced to begin a new line.

It seems to work on Outlook 2010. Please test if this works on other versions.

java - How do I format a String in an email so Outlook will print the ...

java email outlook newline line-breaks