Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 1 | # -*- Mode: Python; tab-width: 4 -*- |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 2 | # Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 3 | # Author: Sam Rushing <rushing@nightmare.com> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 4 | |
| 5 | # ====================================================================== |
| 6 | # Copyright 1996 by Sam Rushing |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 7 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 8 | # All Rights Reserved |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 9 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 10 | # Permission to use, copy, modify, and distribute this software and |
| 11 | # its documentation for any purpose and without fee is hereby |
| 12 | # granted, provided that the above copyright notice appear in all |
| 13 | # copies and that both that copyright notice and this permission |
| 14 | # notice appear in supporting documentation, and that the name of Sam |
| 15 | # Rushing not be used in advertising or publicity pertaining to |
| 16 | # distribution of the software without specific, written prior |
| 17 | # permission. |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 18 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 19 | # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 20 | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN |
| 21 | # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 22 | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 23 | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 25 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 26 | # ====================================================================== |
| 27 | |
Guido van Rossum | e4a1b6d | 2001-04-06 15:30:33 +0000 | [diff] [blame] | 28 | r"""A class supporting chat-style (command/response) protocols. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 29 | |
| 30 | This class adds support for 'chat' style protocols - where one side |
| 31 | sends a 'command', and the other sends a response (examples would be |
| 32 | the common internet protocols - smtp, nntp, ftp, etc..). |
| 33 | |
| 34 | The handle_read() method looks at the input stream for the current |
| 35 | 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n' |
| 36 | for multi-line output), calling self.found_terminator() on its |
| 37 | receipt. |
| 38 | |
| 39 | for example: |
| 40 | Say you build an async nntp client using this class. At the start |
| 41 | of the connection, you'll have self.terminator set to '\r\n', in |
| 42 | order to process the single-line greeting. Just before issuing a |
| 43 | 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST |
| 44 | command will be accumulated (using your own 'collect_incoming_data' |
| 45 | method) up to the terminator, and then control will be returned to |
| 46 | you - by calling your self.found_terminator() method. |
| 47 | """ |
| 48 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 49 | import socket |
| 50 | import asyncore |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 51 | from collections import deque |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 53 | class async_chat (asyncore.dispatcher): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 54 | """This is an abstract class. You must derive from this class, and add |
| 55 | the two methods collect_incoming_data() and found_terminator()""" |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 56 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 57 | # these are overridable defaults |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 58 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 59 | ac_in_buffer_size = 4096 |
| 60 | ac_out_buffer_size = 4096 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 61 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 62 | def __init__ (self, conn=None): |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 63 | # for string terminator matching |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 64 | self.ac_in_buffer = '' |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 65 | |
| 66 | # we use a list here rather than cStringIO for a few reasons... |
| 67 | # del lst[:] is faster than sio.truncate(0) |
| 68 | # lst = [] is faster than sio.truncate(0) |
| 69 | # cStringIO will be gaining unicode support in py3k, which |
| 70 | # will negatively affect the performance of bytes compared to |
| 71 | # a ''.join() equivalent |
| 72 | self.incoming = [] |
| 73 | |
| 74 | # we toss the use of the "simple producer" and replace it with |
| 75 | # a pure deque, which the original fifo was a wrapping of |
| 76 | self.producer_fifo = deque() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 77 | asyncore.dispatcher.__init__ (self, conn) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 78 | |
Andrew M. Kuchling | 7dd5f3c | 2002-03-08 18:27:11 +0000 | [diff] [blame] | 79 | def collect_incoming_data(self, data): |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 80 | raise NotImplementedError("must be implemented in subclass") |
| 81 | |
| 82 | def _collect_incoming_data(self, data): |
| 83 | self.incoming.append(data) |
| 84 | |
| 85 | def _get_data(self): |
| 86 | d = ''.join(self.incoming) |
| 87 | del self.incoming[:] |
| 88 | return d |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 89 | |
Andrew M. Kuchling | 7dd5f3c | 2002-03-08 18:27:11 +0000 | [diff] [blame] | 90 | def found_terminator(self): |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 91 | raise NotImplementedError("must be implemented in subclass") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 92 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 93 | def set_terminator (self, term): |
| 94 | "Set the input delimiter. Can be a fixed string of any length, an integer, or None" |
| 95 | self.terminator = term |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 96 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 97 | def get_terminator (self): |
| 98 | return self.terminator |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 99 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 100 | # grab some more data from the socket, |
| 101 | # throw it to the collector method, |
| 102 | # check for the terminator, |
| 103 | # if found, transition to the next state. |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 104 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 105 | def handle_read (self): |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 106 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 107 | try: |
| 108 | data = self.recv (self.ac_in_buffer_size) |
| 109 | except socket.error, why: |
| 110 | self.handle_error() |
| 111 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 112 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 113 | self.ac_in_buffer = self.ac_in_buffer + data |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 114 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 115 | # Continue to search for self.terminator in self.ac_in_buffer, |
| 116 | # while calling self.collect_incoming_data. The while loop |
| 117 | # is necessary because we might read several data+terminator |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 118 | # combos with a single recv(4096). |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 119 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 120 | while self.ac_in_buffer: |
| 121 | lb = len(self.ac_in_buffer) |
| 122 | terminator = self.get_terminator() |
Andrew M. Kuchling | ca69f02 | 2005-06-09 14:59:45 +0000 | [diff] [blame] | 123 | if not terminator: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 124 | # no terminator, collect it all |
| 125 | self.collect_incoming_data (self.ac_in_buffer) |
| 126 | self.ac_in_buffer = '' |
Andrew M. Kuchling | ca69f02 | 2005-06-09 14:59:45 +0000 | [diff] [blame] | 127 | elif isinstance(terminator, int) or isinstance(terminator, long): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 128 | # numeric terminator |
| 129 | n = terminator |
| 130 | if lb < n: |
| 131 | self.collect_incoming_data (self.ac_in_buffer) |
| 132 | self.ac_in_buffer = '' |
| 133 | self.terminator = self.terminator - lb |
| 134 | else: |
| 135 | self.collect_incoming_data (self.ac_in_buffer[:n]) |
| 136 | self.ac_in_buffer = self.ac_in_buffer[n:] |
| 137 | self.terminator = 0 |
| 138 | self.found_terminator() |
| 139 | else: |
| 140 | # 3 cases: |
| 141 | # 1) end of buffer matches terminator exactly: |
| 142 | # collect data, transition |
| 143 | # 2) end of buffer matches some prefix: |
| 144 | # collect data to the prefix |
| 145 | # 3) end of buffer does not match any prefix: |
| 146 | # collect data |
| 147 | terminator_len = len(terminator) |
Tim Peters | b5d1392 | 2001-04-05 22:38:32 +0000 | [diff] [blame] | 148 | index = self.ac_in_buffer.find(terminator) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 149 | if index != -1: |
| 150 | # we found the terminator |
| 151 | if index > 0: |
| 152 | # don't bother reporting the empty string (source of subtle bugs) |
| 153 | self.collect_incoming_data (self.ac_in_buffer[:index]) |
| 154 | self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:] |
| 155 | # This does the Right Thing if the terminator is changed here. |
| 156 | self.found_terminator() |
| 157 | else: |
| 158 | # check for a prefix of the terminator |
| 159 | index = find_prefix_at_end (self.ac_in_buffer, terminator) |
| 160 | if index: |
| 161 | if index != lb: |
| 162 | # we found a prefix, collect up to the prefix |
| 163 | self.collect_incoming_data (self.ac_in_buffer[:-index]) |
| 164 | self.ac_in_buffer = self.ac_in_buffer[-index:] |
| 165 | break |
| 166 | else: |
| 167 | # no prefix, collect it all |
| 168 | self.collect_incoming_data (self.ac_in_buffer) |
| 169 | self.ac_in_buffer = '' |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 170 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 171 | def handle_write (self): |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 172 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 173 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 174 | def handle_close (self): |
| 175 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 176 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 177 | def push (self, data): |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 178 | sabs = self.ac_out_buffer_size |
| 179 | if len(data) > sabs: |
| 180 | for i in xrange(0, len(data), sabs): |
| 181 | self.producer_fifo.append(data[i:i+sabs]) |
| 182 | else: |
| 183 | self.producer_fifo.append(data) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 184 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 185 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 186 | def push_with_producer (self, producer): |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 187 | self.producer_fifo.append(producer) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 188 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 189 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 190 | def readable (self): |
| 191 | "predicate for inclusion in the readable for select()" |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 192 | # cannot use the old predicate, it violates the claim of the |
| 193 | # set_terminator method. |
| 194 | |
| 195 | # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size) |
| 196 | return 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 197 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 198 | def writable (self): |
| 199 | "predicate for inclusion in the writable for select()" |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 200 | return self.producer_fifo or (not self.connected) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 201 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 202 | def close_when_done (self): |
| 203 | "automatically close this channel once the outgoing queue is empty" |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 204 | self.producer_fifo.append(None) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 205 | |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 206 | def initiate_send(self): |
| 207 | while self.producer_fifo and self.connected: |
| 208 | first = self.producer_fifo[0] |
| 209 | # handle empty string/buffer or None entry |
| 210 | if not first: |
| 211 | del self.producer_fifo[0] |
| 212 | if first is None: |
| 213 | self.handle_close() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 214 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 215 | |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 216 | # handle classic producer behavior |
| 217 | obs = self.ac_out_buffer_size |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 218 | try: |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 219 | data = buffer(first, 0, obs) |
| 220 | except TypeError: |
| 221 | data = first.more() |
| 222 | if data: |
| 223 | self.producer_fifo.appendleft(data) |
| 224 | else: |
| 225 | del self.producer_fifo[0] |
| 226 | continue |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 227 | |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 228 | # send the data |
| 229 | try: |
| 230 | num_sent = self.send(data) |
| 231 | except socket.error: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 232 | self.handle_error() |
| 233 | return |
| 234 | |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 235 | if num_sent: |
| 236 | if num_sent < len(data) or obs < len(first): |
| 237 | self.producer_fifo[0] = first[num_sent:] |
| 238 | else: |
| 239 | del self.producer_fifo[0] |
| 240 | # we tried to send some actual data |
| 241 | return |
| 242 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 243 | def discard_buffers (self): |
| 244 | # Emergencies only! |
| 245 | self.ac_in_buffer = '' |
Josiah Carlson | 1a72d88 | 2008-06-10 05:00:08 +0000 | [diff] [blame] | 246 | del self.incoming[:] |
| 247 | self.producer_fifo.clear() |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 249 | class simple_producer: |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 250 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 251 | def __init__ (self, data, buffer_size=512): |
| 252 | self.data = data |
| 253 | self.buffer_size = buffer_size |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 254 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 255 | def more (self): |
| 256 | if len (self.data) > self.buffer_size: |
| 257 | result = self.data[:self.buffer_size] |
| 258 | self.data = self.data[self.buffer_size:] |
| 259 | return result |
| 260 | else: |
| 261 | result = self.data |
| 262 | self.data = '' |
| 263 | return result |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 264 | |
| 265 | class fifo: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 266 | def __init__ (self, list=None): |
| 267 | if not list: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 268 | self.list = deque() |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 269 | else: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 270 | self.list = deque(list) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 271 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 272 | def __len__ (self): |
| 273 | return len(self.list) |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 274 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 275 | def is_empty (self): |
Armin Rigo | b562bc6 | 2004-09-27 17:49:00 +0000 | [diff] [blame] | 276 | return not self.list |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 277 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 278 | def first (self): |
Raymond Hettinger | 0a4977c | 2004-03-01 23:16:22 +0000 | [diff] [blame] | 279 | return self.list[0] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 280 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 281 | def push (self, data): |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 282 | self.list.append(data) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 283 | |
| 284 | def pop (self): |
| 285 | if self.list: |
Raymond Hettinger | ac093c6 | 2004-02-07 03:19:10 +0000 | [diff] [blame] | 286 | return (1, self.list.popleft()) |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 287 | else: |
| 288 | return (0, None) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 289 | |
| 290 | # Given 'haystack', see if any prefix of 'needle' is at its end. This |
| 291 | # assumes an exact match has already been checked. Return the number of |
| 292 | # characters matched. |
| 293 | # for example: |
| 294 | # f_p_a_e ("qwerty\r", "\r\n") => 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 295 | # f_p_a_e ("qwertydkjf", "\r\n") => 0 |
Andrew M. Kuchling | c63a396 | 2002-03-20 02:22:58 +0000 | [diff] [blame] | 296 | # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 297 | |
| 298 | # this could maybe be made faster with a computed regex? |
Andrew M. Kuchling | d305f51 | 2001-01-24 21:10:55 +0000 | [diff] [blame] | 299 | # [answer: no; circa Python-2.0, Jan 2001] |
Andrew M. Kuchling | c63a396 | 2002-03-20 02:22:58 +0000 | [diff] [blame] | 300 | # new python: 28961/s |
| 301 | # old python: 18307/s |
Andrew M. Kuchling | d305f51 | 2001-01-24 21:10:55 +0000 | [diff] [blame] | 302 | # re: 12820/s |
| 303 | # regex: 14035/s |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 304 | |
| 305 | def find_prefix_at_end (haystack, needle): |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 306 | l = len(needle) - 1 |
| 307 | while l and not haystack.endswith(needle[:l]): |
| 308 | l -= 1 |
| 309 | return l |