<commit_before># Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.db.models.lookups import (
    EndsWith, IEndsWith, IStartsWith, StartsWith,
)


def endswith(self, compiler, connection):
    lhs_sql, params = self.process_lhs(compiler, connection)
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    params.extend(rhs_params)
    rhs_sql = self.get_rhs_op(connection, rhs_sql)
    # Chop leading '%' from param since this isn't a LIKE query.
    params[0] = params[0][1:]
    # rhs_sql is 'STARTS_WITH(%s, %%s)' and lhs_sql is the column name.
    return rhs_sql % lhs_sql, params


def startswith(self, compiler, connection):
    # Same logic as endswith but chop trailing (rather than leading) '%' from
    # param.
    lhs_sql, params = self.process_lhs(compiler, connection)
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    params.extend(rhs_params)
    rhs_sql = self.get_rhs_op(connection, rhs_sql)
    params[0] = params[0][:-1]
    return rhs_sql % lhs_sql, params


def istartswith(self, compiler, connection):
    lhs_sql, params = self.process_lhs(compiler, connection)
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    params.extend(rhs_params)
    rhs_sql = self.get_rhs_op(connection, rhs_sql)
    params[0] = "^(?i)%s" % params[0][:-1]
    return rhs_sql % lhs_sql, params


def iendswith(self, compiler, connection):
    lhs_sql, params = self.process_lhs(compiler, connection)
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    params.extend(rhs_params)
    rhs_sql = self.get_rhs_op(connection, rhs_sql)
    params[0] = "(?i)%s$" % params[0][1:]
    return rhs_sql % lhs_sql, params


def register_lookups():
    EndsWith.as_spanner = endswith
    IEndsWith.as_spanner = iendswith
    StartsWith.as_spanner = startswith
    IStartsWith.as_spanner = istartswith<commit_msg>django: unify startswith, endswith, istartswith, and iendswith lookups<commit_after># Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from django.db.models.lookups import (
    EndsWith, IEndsWith, IStartsWith, StartsWith,
)


def startswith_endswith(self, compiler, connection):
    """startswith, endswith, istartswith, and iendswith lookups."""
    lhs_sql, params = self.process_lhs(compiler, connection)
    rhs_sql, rhs_params = self.process_rhs(compiler, connection)
    params.extend(rhs_params)
    rhs_sql = self.get_rhs_op(connection, rhs_sql)
    # Chop the leading (endswith) or trailing (startswith) percent sign that
    # Django adds to the param since this isn't a LIKE query as Django expects.
    if 'endswith' in self.lookup_name:
        params[0] = params[0][1:]
    else:
        params[0] = params[0][:-1]
    # Construct the regex for istartswith or iendswith.
    if self.lookup_name.startswith('i'):
        regex = '^(?i)%s' if self.lookup_name == 'istartswith' else '(?i)%s$'
        params[0] = regex % params[0]
    # rhs_sql is e.g. 'STARTS_WITH(%s, %%s)' or REGEXP_CONTAINS(%s, %%s), and
    # lhs_sql is the column name.
    return rhs_sql % lhs_sql, params


def register_lookups():
    EndsWith.as_spanner = startswith_endswith
    IEndsWith.as_spanner = startswith_endswith
    StartsWith.as_spanner = startswith_endswith
    IStartsWith.as_spanner = startswith_endswith
{'fraction_non_alphanumeric': 0.09870848708487084, 'fraction_numerical': 0.008302583025830259, 'mean_word_length': 4.181600955794504, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3363608', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1411, 'n_words': 420}