• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Word count
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Word count


  • Subject: Re: Word count
  • From: Allan Odgaard <email@hidden>
  • Date: Thu, 10 Jun 2004 06:31:31 +0200

On 10. Jun 2004, at 5:26, Matt Baker wrote:

So, for anyone who's run into counting / parsing, what's the quickest way to accurately count words with NSScanner (or anything else in Cocoa)?

Well, here's a generic C++ function to count words:

template <typename InputIter, typename Predicate>
size_t word_count (InputIter first, InputIter last, Predicate word_char)
{
size_t res = 0;
while(first != last)
{
InputIter bow = std::find_if(first, last, word_char);
InputIter eow = std::find_if(bow, last, std::not1(word_char));
if(bow != eow)
++res;
first = eow;
}
return res;
}

Using CocoaSTL it can be used on an NSString like this:

NSString* str = @"Hello, world!";
size_t cnt = word_count(beginof(str), endof(str),
std::bind1st(unary_method<BOOL, unichar>("characterIsMember:"),
[NSCharacterSet letterCharacterSet]));

The third argument is just a fancy way to construct a function on the fly to return true when handed a unicode character in the letter character set :)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Word count (From: Matt Baker <email@hidden>)
 >Re: Word count (From: j o a r <email@hidden>)
 >Re: Word count (From: Matt Baker <email@hidden>)

  • Prev by Date: Probably a silly question but...
  • Next by Date: Re: NSOutlineView Problem
  • Previous by thread: Re: Word count
  • Next by thread: Re: Word count
  • Index(es):
    • Date
    • Thread