From patchwork Tue May 1 23:03:10 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Barker X-Patchwork-Id: 8340 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 433B423E49 for ; Tue, 1 May 2012 23:03:14 +0000 (UTC) Received: from mail-yx0-f180.google.com (mail-yx0-f180.google.com [209.85.213.180]) by fiordland.canonical.com (Postfix) with ESMTP id D9DF1A181BA for ; Tue, 1 May 2012 23:03:13 +0000 (UTC) Received: by yenl4 with SMTP id l4so49604yen.11 for ; Tue, 01 May 2012 16:03:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf :content-type:mime-version:x-launchpad-project:x-launchpad-branch :x-launchpad-message-rationale:x-launchpad-branch-revision-number :x-launchpad-notification-type:to:from:subject:message-id:date :reply-to:sender:errors-to:precedence:x-generated-by :x-launchpad-hash:x-gm-message-state; bh=hVPWCK3pzng1aKS4YjXFwuoJFDCVCFgeJXpRXgaKolk=; b=JEwSJNSyNpuC2o4Qrentg7jABLIlhkv6x44tesI94p4Vafwh8biShSZGVBJW0Vb9G7 qM7u/nxAvMfpjupkc9CiTL8oYth+otF6nBYv4ghIQIU/BjfKRqjdfWPrsM+xGfiLwtgA rhLkqB+jvhUC+bmkZ+2h/NXb47foxjj39csvmrzBhhpGFF+1ZihgL/npCngfysCu6Ms7 3rLkb6fV+Rf5ADEmv8INKw1vjDUmrHGvFKxcYyObNUnWsQ7lXPOUgh36MxzjiL72hp0j gB4C9JHVE3cgHBGnzY0HC8Uc5n8YE7tvFCE4ztyFXy/wEM9vKum0Qne6IJyrO5QOFMNe um1Q== Received: by 10.50.217.230 with SMTP id pb6mr3283418igc.1.1335913393179; Tue, 01 May 2012 16:03:13 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.137.198 with SMTP id x6csp212214ibt; Tue, 1 May 2012 16:03:11 -0700 (PDT) Received: by 10.180.103.35 with SMTP id ft3mr8656986wib.0.1335913390868; Tue, 01 May 2012 16:03:10 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id j9si19377805wia.32.2012.05.01.16.03.10 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 May 2012 16:03:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1SPM6A-00010e-6Z for ; Tue, 01 May 2012 23:03:10 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id 1D597E023D for ; Tue, 1 May 2012 23:03:10 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: libmatrix X-Launchpad-Branch: ~jesse-barker/libmatrix/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 37 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~jesse-barker/libmatrix/trunk] Rev 37: Util: Update split() to treat the delimiter more flexibly, so that if there is Message-Id: <20120501230310.11085.37681.launchpad@ackee.canonical.com> Date: Tue, 01 May 2012 23:03:10 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="15177"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: ff16606ae12cc5a684906f79c9a3a1ecf69f1516 X-Gm-Message-State: ALoCoQmHT+N3K7Y/uJXmr8ILcmCIVNoQhYv52ghOzvJ5mpY5UXvZsmo2UNm/Qy69H20JsLjANG14 ------------------------------------------------------------ revno: 37 committer: Jesse Barker branch nick: trunk timestamp: Tue 2012-05-01 15:58:28 -0700 message: Util: Update split() to treat the delimiter more flexibly, so that if there is whitespace or consecutive delimiters, the elements come out cleanly. modified: util.cc util.h --- lp:libmatrix https://code.launchpad.net/~jesse-barker/libmatrix/trunk You are subscribed to branch lp:libmatrix. To unsubscribe from this branch go to https://code.launchpad.net/~jesse-barker/libmatrix/trunk/+edit-subscription === modified file 'util.cc' --- util.cc 2012-01-27 21:58:38 +0000 +++ util.cc 2012-05-01 22:58:28 +0000 @@ -22,21 +22,46 @@ #include "log.h" #include "util.h" -/** - * Splits a string using a delimiter - * - * @param s the string to split - * @param delim the delimitir to use - * @param elems the string vector to populate - */ +using std::string; +using std::vector; + void -Util::split(const std::string &s, char delim, std::vector &elems) +Util::split(const string& src, char delim, vector& elementVec) { - std::stringstream ss(s); - - std::string item; - while(std::getline(ss, item, delim)) - elems.push_back(item); + if (src.empty()) + { + return; + } + // Initialize our delimiter string based upon the caller's plus a space + // to allow for more flexibility. + string delimiter(" "); + delimiter += delim; + // Starting index into the string of the first token (by definition, if + // we're parsing a string, there is at least one token). + string::size_type startPos(0); + // string::find_first_of() looks for any character in the string provided, + // it is not treated as a sub-string, so regardless of where the space or + // comma is or how many there are, the result is the same. + string str(src); + string::size_type endPos = str.find_first_of(delimiter); + while (endPos != string::npos) + { + // Push back the current element starting at startPos for + // (endPos - startPos) characters. std::string takes care of + // terminators, etc. + elementVec.push_back(string(str, startPos, endPos - startPos)); + // Index of the next element after any delimiter characters. Same + // caveat applies to find_first_not_of() that applies to + // find_first_of(); endPos tells it where to start the search. + string::size_type nextPos = str.find_first_not_of(delimiter, endPos); + // Erase the part of the string we've already parsed. + str = str.erase(startPos, nextPos - startPos); + // Look for the next delimiter. If there isn't one, we bail out. + endPos = str.find_first_of(delimiter); + } + // Regardless of whether we initially had one element or many, 'str' now + // only contains one. + elementVec.push_back(str); } uint64_t === modified file 'util.h' --- util.h 2012-01-27 21:58:38 +0000 +++ util.h 2012-05-01 22:58:28 +0000 @@ -24,7 +24,23 @@ #endif struct Util { + /** + * split() - Splits a string into elements separated by a delimiter + * + * @s: the string to split + * @delim: the delimiter to use + * @elems: the string vector to populate + * + * Splits @s into a vector of string elements, delimited by @delim, that + * get returned in @elems. The handling of @delim allows for spaces on + * either side of the delimiter or even multiple consecutive delimiters + * with no elements between. As long as @s is non-empty, there will be + * at least one element in @elems. + */ static void split(const std::string &s, char delim, std::vector &elems); + /** + * get_timestamp_us() - Returns the current time in microseconds + */ static uint64_t get_timestamp_us(); static std::istream *get_resource(const std::string &path); static void list_files(const std::string& dirName, std::vector& fileVec); @@ -44,7 +60,7 @@ fromString(const std::string& asString) { std::stringstream ss(asString); - T retVal; + T retVal = T(); ss >> retVal; return retVal; }