String源码阅读之contains实现原理

转载请注明出处:http://blog.csdn.net/jevonsCSDN/article/details/60758197 【Jevons’Blog】

本文将对String部分源码进行阅读分析的记录。

contains

对String中的contains方法进行分析,了解其采用的是什么算法进行匹配。

//用于判断源字符串是否包含目标字符序列 CharSequence s
 public boolean contains(CharSequence s) {
         //调用indexOf(String str)方法
        return indexOf(s.toString()) > -1;
    }

indexOf (String str)

    public int indexOf(String str) {
    //继续往下调用indexOf(String str, int fromIndex)方法,并传入匹配起始下标
        return indexOf(str, 0);
    }

indexOf (String str, int fromIndex)

    public int indexOf(String str, int fromIndex) {
        //继续往下调用indexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex) 函数
        //传入源字符串,源字符串偏移量,源字符串长度,目标字符串,目标字符串偏移量,目标字符串长度,匹配源字符串的起始坐标
        return indexOf(value, 0, value.length,
                str.value, 0, str.value.length, fromIndex);
    }

indexOf (char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex)

static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
         //若匹配的起始下标大于或等于源字符串的长度
        if (fromIndex >= sourceCount) {
            //检测目标长度是否为0,是则返回源字符串长度,否则返回-1
            return (targetCount == 0 ? sourceCount : -1);
        }
        //若匹配的起始下标小于0
        if (fromIndex < 0) {
            //将匹配的起始下标置0
            fromIndex = 0;
        }
        //若目标字符串长度等于0
        if (targetCount == 0) {
            //直接返回匹配的起始下标
            return fromIndex;
        }
        //从定义的目标偏移量中取出目标字符串的第一个字符
        char first = target[targetOffset];
        //获取源字符串能被匹配的最大长度
        int max = sourceOffset + (sourceCount - targetCount);
        //从定义的偏移量加上起始匹配下标开始进行匹配
        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            //检测第一个字符是否相等
            if (source[i] != first) {
                //不相等则循环匹配,直到找到能与目标字符串第一个字符匹配的源字符串下标。
                while (++i <= max && source[i] != first);
            }

            /* Found first character, now look at the rest of v2 */
            //已经找到了与目标字符串第一个字符匹配的源字符串下标,则从该下标的下一位开始,对目标字符串余下的字符进行匹配。
            if (i <= max) {
                //从该下标的下一位开始
                int j = i + 1;
                //定义本次匹配的最大长度
                int end = j + targetCount - 1;
                //循环匹配
                for (int k = targetOffset + 1; j < end && source[j]
                        == target[k]; j++, k++);
                //j能一直增加到end,说明已经成功匹配
                if (j == end) {
                    /* Found whole string. */
                    //返回在源字符串中被匹配到的第一个字符的下标。
                    return i - sourceOffset;
                }
            }
        }
        //没有匹配到,返回-1
        return -1;
    }

  从这个方法中可以看出,其采用的是最原始也是最笨的方法进行匹配,将目标字符串与源字符串的字符进行逐一匹配,若第一个字符匹配成功,则进行余下的匹配,若余下的匹配不成功,则从一开始目标字符串的第一个字符与源字符串匹配时的下标的下一位继续进行匹配。这样其实是浪费了很多没必要的时间。有关字符串匹配的快捷有效匹配算法,可以参照KMP算法

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
========================================== NO ONE LIVES FOREVER Source Code v1.003 May 15, 2001 ========================================== I. INSTALLATION INSTRUCTIONS II. SYSTEM REQUIREMENTS III. DIRECTORY STRUCTURE IV. NOLF VISUAL C++ WORKSPACE V. END USER LISCENCE AGREEMENT (EULA) The No One Lives Forever (NOLF) Source Code v1.003 is not officially supported by Monolith Productions, Inc. or Fox Interactive. If you plan to modify the NOLF Source Code it is assumed that you have a good understanding of the NOLF tools and how Monolith games work (i.e., you have worked with the Shogo or Blood 2 source code). NOTE: The source code is specifically designed to work with NOLF version 1.003. Using modifications made with it on older versions of NOLF may lead to unpredictable results. I. INSTALLATION INSTRUCTIONS __________________________________ To install the NOLF source code, click on the archive and extract the files to a directory of your choosing. The default path is c:\NOLFSource. Once the extraction is complete, you can open the NOLF Visual C++ workspace by double clicking on the file c:\NOLFSource\NOLF\NOLF.dsw. II. SYSTEM REQUIREMENTS __________________________________ To build the NOLF source v1.003 you will need the following: Visual C++ 6.0 with service pack 3 installed DX8 sdk 400MB free disk space III. DIRECTORY STRUCTURE __________________________________ The NOLF source code extracts into the following directory structure: c:\NOLFSource (Directory you extracted to) NOLF (Root game code directory) ClientRes (String resource dll) ClientShellDLL (Client dll) ObjectDLL (Server dll) Shared (Source code used by both the client and the server) LT2 (Root engine code directory) Lithshared (Header files for shared Monolith libraries) Sdk (LithTech sdk header files) III. NOLF VISUAL C++ WORKSPACE __________________________________ The NOLF Visual C++ Workspace (NOLF.dsw) contains the ClientRes, ClientShellDLL, and Object projects. These three projects compile into the cres.dll, cshell.dll, and object.lto dlls, respectively. Once compiled, new versions of cres.dll, cshell.dll, and object.lto should be placed in the root of your NOLF resource directory to be used by the game (i.e., in the root of the NOLF.rez file). The ClientShellDLL and Object projects contain Final and Final Debug build targets. These build targets MUST be used when building the game for use with .rez files. However, you can use the normal Debug and Release builds if you are running NOLF from a normal directory structure (i.e., you have unrezed NOLF.rez, NOLF2.rez, etc. and are running the game from this directory). IV. END-USER LICENSE AGREEMENT FOR ADD-ON COMPONENTS -- NO ONE LIVES FOREVER Source Code v1.003 ________________________________________________________________________________________________ THIS END-USER LICENSE AGREEMENT (THIS "EULA") IS A LEGAL AGREEMENT BETWEEN YOU (EITHER AN INDIVIDUAL OR A SINGLE ENTITY) AND MONOLITH PRODUCTIONS, INC. ("MONOLITH") FOR NO ONE LIVES FOREVER Source Code v1.003, WHICH INCLUDES COMPUTER SOFTWARE AND ASSOCIATED MATERIALS (THE "SOFTWARE"). YOU MAY ONLY BECOME A PARTY TO THIS AGREEMENT IF YOU ARE A LICENSED USER OF MONOLITH'S The Operative: "No One Lives Forever" PRODUCT. BY CLICKING ON THE "YES" BUTTON, YOU ARE CONSENTING TO BE BOUND BY AND ARE BECOMING A PARTY TO THIS AGREEMENT. IF YOU DO NOT AGREE TO ALL OF THE TERMS OF THIS AGREEMENT, CLICK THE "NO" BUTTON AND DO NOT USE THE SOFTWARE. 1. GRANT OF LICENSE. You may use and publicly display one copy of the Software on one personal computer. A license for the Software may not be shared or used concurrently on different computers. 2. LIMITATIONS. You may not reverse engineer, decompile, or disassemble the Software. You may not rent or lease the Software. You may make one additional copy of the Software solely for backup or archival purposes. You may not copy any printed materials accompanying the Software. 3. SOFTWARE TRANSFER. You may permanently transfer all of your rights under this EULA, provided (a) you retain no copies, (b) you transfer all of the Software (including all component parts, the media on which the Software was delivered, all printed materials, any upgrades, and this EULA), and (c) the recipient agrees to the terms of this EULA in writing delivered to Monolith. Any transfer must include all prior versions and upgrades of the Software. Otherwise, you have no right to distribute copies of the Software. 4. TERMINATION. Without prejudice to any other rights, Monolith may terminate this EULA if you fail to comply with the terms and conditions of this EULA. In such event, you must destroy all copies of the Software and all of its component parts. 5. NO SUPPORT. Because Monolith is offering this Software to you free of charge, Monolith will not provide any telephone, online or other support for the use of the Software. 6. NO WARRANTIES. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MONOLITH AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH REGARD TO THE SOFTWARE. 7. NO LIABILITY FOR DAMAGES. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL MONOLITH OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE PRODUCT, EVEN IF MONOLITH HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME STATES DO NOT ALLOW SUCH EXCLUSION OR LIMITATION OF LIABILITY, THE ABOVE LIMITATION MAY NOT APPLY TO YOU. 8. EDITOR, SOURCE CODE AND END-USER VARIATIONS. (a) The Software may include an "Editor." An "Editor" is a feature which allows you to modify the Software or to construct new variations for use with it. These modifications and variations can be both playable and non-playable. An Editor includes its associated tools and utilities. An Editor is NOT shareware. You may not freely distribute it to any BBS, CD, floppy or any other media. You may not sell it or repackage it for sale. (b) The Software may include "Source Code." "Source Code" is a feature which allows you to modify the Software or to construct new variations for use with it. These modifications and variations can be both playable and non-playable. Source Code is NOT public domain. You may not freely distribute it to any BBS, CD, floppy or any other media. You may not sell it or repackage it for sale. (c) Using the Editor and/or Source Code, you may create modifications or enhancements to the Software, including the construction of new levels (collectively referred to as "Variations"), subject to the following restrictions: i. Your Variations must only work with the full copy of the Software, not independently or with any other software. ii. Your Variations must not contain modifications to any executable file. iii. Your Variations must not contain any libelous, defamatory, or other illegal material, material that is scandalous or invades the rights of privacy or publicity of any third party, or contains any trademarks, copyright-protected work, or other recognizable property of third parties. iv. At least once in every online description and with reasonable duration on the opening screen, your Variations must prominently identify (i) the names and email addresses of its creators, and (ii) the words "This add-on is not made by or supported by Monolith Productions, or any of its affiliates and subsidiaries." v. Your Variations must be distributed solely for free. Neither you nor any other person or party may sell them to anyone, commercially exploit them in any way, or charge anyone for using them. You may exchange them at no charge among other end-users. vi. By distributing or permitting the distribution of any of your Variations, you hereby grant back to Monolith Productions an irrevocable royalty-free right to use and distribute them by any means. vii. The prohibitions and restrictions in this section apply to anyone in possession of the Software or any of your Variations. 9. MISCELLANEOUS. This EULA is governed by the laws of the State of Washington, U.S.A. Each of the parties hereto submits to jurisdiction in the state and federal courts sitting in King County, Washington. Should you have any questions concerning this EULA, or if you desire to contact Monolith for any reason, please write: Monolith Productions, Inc., 10516 NE 37th Circle, Kirkland, WA 98033.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值