VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/string/public/nsAlgorithm.h@ 48392

Last change on this file since 48392 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Scott Collins <scc@mozilla.org> (original author)
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#ifndef nsAlgorithm_h___
40#define nsAlgorithm_h___
41
42#ifndef nsCharTraits_h___
43#include "nsCharTraits.h"
44 // for |nsCharSourceTraits|, |nsCharSinkTraits|
45#endif
46
47#ifndef prtypes_h___
48#include "prtypes.h"
49 // for |PRUint32|...
50#endif
51
52#ifndef nsDebug_h___
53#include "nsDebug.h"
54 // for NS_ASSERTION
55#endif
56
57template <class T>
58inline
59const T&
60NS_MIN( const T& a, const T& b )
61 {
62 return b < a ? b : a;
63 }
64
65template <class T>
66inline
67const T&
68NS_MAX( const T& a, const T& b )
69 {
70 return a > b ? a : b;
71 }
72
73template <class InputIterator, class T>
74inline
75PRUint32
76NS_COUNT( InputIterator& first, const InputIterator& last, const T& value )
77 {
78 PRUint32 result = 0;
79 for ( ; first != last; ++first )
80 if ( *first == value )
81 ++result;
82 return result;
83 }
84
85template <class InputIterator, class OutputIterator>
86inline
87OutputIterator&
88copy_string( InputIterator& first, const InputIterator& last, OutputIterator& result )
89 {
90 typedef nsCharSourceTraits<InputIterator> source_traits;
91 typedef nsCharSinkTraits<OutputIterator> sink_traits;
92
93 while ( first != last )
94 {
95 PRInt32 count_copied = PRInt32(sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last)));
96 NS_ASSERTION(count_copied > 0, "|copy_string| will never terminate");
97 source_traits::advance(first, count_copied);
98 }
99
100 return result;
101 }
102
103template <class InputIterator, class OutputIterator>
104OutputIterator&
105copy_string_backward( const InputIterator& first, InputIterator& last, OutputIterator& result )
106 {
107 while ( first != last )
108 {
109 last.normalize_backward();
110 result.normalize_backward();
111 PRUint32 lengthToCopy = PRUint32( NS_MIN(last.size_backward(), result.size_backward()) );
112 if ( first.fragment().mStart == last.fragment().mStart )
113 lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.get() - first.get()));
114
115 NS_ASSERTION(lengthToCopy, "|copy_string_backward| will never terminate");
116
117#ifdef _MSC_VER
118 // XXX Visual C++ can't stomach 'typename' where it rightfully should
119 nsCharTraits<OutputIterator::value_type>::move(result.get()-lengthToCopy, last.get()-lengthToCopy, lengthToCopy);
120#else
121 nsCharTraits<typename OutputIterator::value_type>::move(result.get()-lengthToCopy, last.get()-lengthToCopy, lengthToCopy);
122#endif
123
124 last.advance( -PRInt32(lengthToCopy) );
125 result.advance( -PRInt32(lengthToCopy) );
126 }
127
128 return result;
129 }
130
131#endif // !defined(nsAlgorithm_h___)
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette